templates - What's the equivalent of front-page.php for a custom static page?

I just added a static page option in Reading Settings with the help of this post. If front-page.php exists, it is used f

I just added a static page option in Reading Settings with the help of this post.

If front-page.php exists, it is used for Homepage. What should I name my template file to ensure that my custom static page always uses that file no matter what the name of the page is?

I just added a static page option in Reading Settings with the help of this post.

If front-page.php exists, it is used for Homepage. What should I name my template file to ensure that my custom static page always uses that file no matter what the name of the page is?

Share Improve this question edited May 30, 2019 at 8:51 fuxia 107k39 gold badges255 silver badges459 bronze badges asked May 30, 2019 at 2:06 SheedoSheedo 1263 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

The Template Hierarchy only covers what's listed here. So there is no filename you can give a template for it to be automatically used by default.

However, it is possible to add your own logic to the template hieararchy with the page_template_hierarchy hook. So using this hook, you could check if the current page being viewed is the page that has been set as your custom static page, and if so, add your own template name to the top of the list.

So, in the accepted answer at your link, the static page is saved as page_for_projects, which means the code would look like this:

function wpse_339118_projects_template_hierarchy( $templates ) {
    $page_id           = get_queried_object_id();
    $page_for_projects = get_option( 'page_for_projects' );

    // If the current page is the Projects page.
    if ( $page_id === $page_for_projects ) {
        // Use projects.php as the template, if it exists.
        array_unshift( $templates, 'projects.php' );
    }

    return $templates; 
}
add_filter( 'page_template_hierarchy', 'wpse_339118_projects_template_hierarchy' );

With that code, if you name your template projects.php, it will be loaded automatically when viewing the page that's saved as your custom static page.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745451720a4628301.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信