admin管理员组

文章数量:1304892

I need to overwrite the show_on_front, page_on_front, page_for_posts options values for some reasons. I am setting show_on_front to 'page' and the other both to two different page id. If I change these settings in Administration > Settings > Reading panel to the same values as I do programmatically, the front page loads the front-page.php template. Otherwise the front page loads the page.php template. What am I doing wrong?

add_filter('pre_option_show_on_front', 'static_front_page');
function static_front_page() {
    return 'page';
}
add_filter('pre_option_page_on_front', 'page_on_front');
function page_on_front() {
    return 123;
}
add_filter('pre_option_page_for_posts', 'page_for_posts');
function page_for_posts() {
    return 123;
}

I know this is not best practice, but would love to get this done this way.

Another problem when setting it per filter, is that is_front_page() isn´t working correct after it.

I need to overwrite the show_on_front, page_on_front, page_for_posts options values for some reasons. I am setting show_on_front to 'page' and the other both to two different page id. If I change these settings in Administration > Settings > Reading panel to the same values as I do programmatically, the front page loads the front-page.php template. Otherwise the front page loads the page.php template. What am I doing wrong?

add_filter('pre_option_show_on_front', 'static_front_page');
function static_front_page() {
    return 'page';
}
add_filter('pre_option_page_on_front', 'page_on_front');
function page_on_front() {
    return 123;
}
add_filter('pre_option_page_for_posts', 'page_for_posts');
function page_for_posts() {
    return 123;
}

I know this is not best practice, but would love to get this done this way.

Another problem when setting it per filter, is that is_front_page() isn´t working correct after it.

Share Improve this question edited Mar 6, 2017 at 17:49 Status4 asked Mar 6, 2017 at 17:35 Status4Status4 337 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Ok, got it working. I was returning a property of a serialized option within the callbacks for the filters:

add_filter('pre_option_page_on_front', 'page_on_front');
function page_on_front() {
    $options = get_option('theme_options');
    return $options['page_on_front'];
}
add_filter('pre_option_page_for_posts', 'page_for_posts');
function page_for_posts() {
    $options = get_option('theme_options');
    return $options['page_for_posts'];
}

The returend values were numbers, but strings. Typecasting them to int got it working.

本文标签: filtersManipulating showonfrontpageonfrontpageforposts and template hierarchy