本文档介绍了 WordPress 中自定义首页模板的实现方式,包括静态首页和博客文章索引页的模板选择、模板层次结构以及相关条件标签的使用。面向 WordPress 主题开发者,旨在帮助创建灵活的前端页面显示。
// 方法1:在 front-page.php 中包含自定义内容
if ( 'posts' == get_option( 'show_on_front' ) ) {
include( get_home_template() );
} else {
// 自定义内容标记放在这里
}
// 方法2:在 front-page.php 中包含任何页面模板
if ( 'posts' == get_option( 'show_on_front' ) ) {
include( get_home_template() );
} else {
include( get_page_template() );
}
// 过滤 frontpage_template 以绕过 front-page.php
function themeslug_filter_front_page_template( $template ) {
return is_home() ? '' : $template;
}
add_filter( 'frontpage_template', 'themeslug_filter_front_page_template' );
// 添加自定义查询循环显示最新博客文章
$latest_blog_posts = new WP_Query( array( 'posts_per_page' => 3 ) );
if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
// 循环输出放在这里
endwhile; endif;By default, WordPress shows your most recent posts in reverse chronological order on the front page of your site. Many WordPress users want a static front page or splash page as the front page instead. This “static front page” look is common for users desiring static or welcoming information on the front page of the site.
The look and feel of the front page of the site is based upon the choices of the user combined with the features and options of the WordPress Theme.
On the site front page, WordPress will always use the front-page.php template file, if it exists. If front-page.php does not exist, WordPress will determine which template file to use, depending on the user configuration of Settings > Reading >Front page displays, as follows:
To create a custom site front page template, include either of the following in the Theme:
To create a custom blog posts index template, include the following in the Theme:
Use only the home.php template file for the blog posts index. Do not use a Custom Page Template (such as template-blog.php) for two reasons:
The Conditional Tag is_front_page() checks if the site front page is being displayed. Returns true when the site front page is being displayed, regardless of whether ‘Settings > Reading ->Front page displays’ is set to “Your latest posts” or “A static page”.
The Conditional Tag is_home() checks if the blog posts index is being displayed. Returns true when the blog posts index is being displayed: when the site front page is being displayed and ‘Settings > Reading ->Front page displays’ is set to “Your latest posts”, or when ‘Settings > Reading ->Front page displays’ is set to “A static page” and the “Posts Page” value is the current Page being displayed.
When the site front page is being displayed and ‘Settings > Reading ->Front page displays’ is set to “Your latest posts”, bothis_front_page() and is_home() will return true.
If it exists, the front-page.php template file is used on the site’s front page regardless of whether ‘Settings > Reading ->Front page displays’ is set to “A static page” or “Your latest posts,” the Theme will need to account for both options, so that the site front page will display either a static page or the blog posts index. There are a few methods to do so.
One way to allow front-page.php to account for both options for ‘Settings > Reading ->Front page displays’ is to add a conditional inside of front-page.php itself, using get_option( 'show_on_front' ), get_home_template(), andget_page_template().
Method 1: including custom content directly within front-page.php:
if ( 'posts' == get_option( 'show_on_front' ) ) {
include( get_home_template() );
} else {
// Custom content markup goes here
}
Method 2: including any page template:
if ( 'posts' == get_option( 'show_on_front' ) ) {
include( get_home_template() );
} else {
include( get_page_template() );
}
Another way to allow the site front page to display either a static page/custom content or the blog posts index, without adding conditional code within front-page.php, is to filter frontpage_template, by adding a filter callback to functions.php:
function themeslug_filter_front_page_template( $template ) {
return is_home() ? '' : $template;
}
add_filter( 'frontpage_template', 'themeslug_filter_front_page_template' );
This method causes WordPress to bypass the front-page.php template file altogether when the blog posts index is being displayed.
If the front-page.php template file includes a default WordPress Loop, like so:
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
// do something
endwhile; else:
// no posts found
endif;
That loop applies to the post content of the static page assigned to ‘Settings > Reading ->Posts page’.
To display custom loops (latest blog posts, custom/featured content, etc.), add secondary loop queries using calls to WP_Query. For example, to show the 3 latest blog posts:
$latest_blog_posts = new WP_Query( array( 'posts_per_page' => 3 ) );
if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
// Loop output goes here
endwhile; endif;
Static front pages are not intended to be paged. None of the WordPress Previous / Next page link functions work with a static front page. Pagination on a static front page uses the page query variable, not the paged variable. See the WP_Query for details.