index.php
<?php
/**
* The main template file
*
* This is the most generic template file in a WordPress theme and one
* of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query,
* e.g., it puts together the home page when no home.php file exists.
*
* @link http://codex.wordpress.org/Template_Hierarchy
*
* @package WordPress
* @subpackage Twenty_Fourteen
* @since Twenty Fourteen 1.0
*/
get_header(); ?>
<div id="main-content" class="main-content">
<?php
if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
整段代码的意思是 如果是首页并post的tag为featured,那么调用featured_content.php模板
twentyfourteen_has_featured_posts函数在functions.php中,源码为
/**
* A helper conditional function that returns a boolean value.
*
* @since Twenty Fourteen 1.0
*
* @return bool Whether there are featured posts.
*/
function twentyfourteen_has_featured_posts() {
return ! is_paged() && (bool) twentyfourteen_get_featured_posts();
}
返回一个布尔值
是否分页
执行twentyfourteen_get_featured_posts()函数,这个函数返回一个数组,如果是空的则是false
/**
* Getter function for Featured Content Plugin.
*
* @since Twenty Fourteen 1.0
*
* @return array An array of WP_Post objects.
*/
function twentyfourteen_get_featured_posts() {
/**
* Filter the featured posts to return in Twenty Fourteen.
*
* @since Twenty Fourteen 1.0
*
* @param array|bool $posts Array of featured posts, otherwise false.
*/
return apply_filters( 'twentyfourteen_get_featured_posts', array() );
}
注册一个名为twentyfourteen_get_featured_posts的过滤器,返回一个空数组。
// Include the featured content template
get_template_part( 'featured-content' );
调用模板 featured_content.php用法为
<?php get_template_part( $slug, $name ) ?>
第一个参数是别名
}
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
/*
* get_template_part 主要是获取子模板
* get_post_format()主要是获取当前文章属于哪种,然后加载相应的模板
* 所以他的模板文件就是content-aside.php,content-chat.php等。
* 如果以上某一个不存在,就会自动调用content.php
*/
endwhile;
// Previous/next post navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->
<?php
get_sidebar();
get_footer();