为了在主页上显示一个广告以及标准帖子,我将其添加到WordPress主循环中:
/** Add "advert" post type to the main query **/
add_action( \'pre_get_posts\', \'add_advert_post_type_to_query\' );
function add_advert_post_type_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( \'post_type\', array( \'post\', \'advert\' ) );
return $query;
}
在
index.php
在我的主题(2017)中,我看到了loop用来显示帖子内容的模板:
<?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 override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( \'template-parts/post/content\', get_post_format() );
endwhile;
the_posts_pagination( array(
\'prev_text\' => twentyseventeen_get_svg( array( \'icon\' => \'arrow-left\' ) ) . \'<span class="screen-reader-text">\' . __( \'Previous page\', \'twentyseventeen\' ) . \'</span>\',
\'next_text\' => \'<span class="screen-reader-text">\' . __( \'Next page\', \'twentyseventeen\' ) . \'</span>\' . twentyseventeen_get_svg( array( \'icon\' => \'arrow-right\' ) ),
\'before_page_number\' => \'<span class="meta-nav screen-reader-text">\' . __( \'Page\', \'twentyseventeen\' ) . \' </span>\',
) );
else :
get_template_part( \'template-parts/post/content\', \'none\' );
endif;
?>
在该模板中(
template-parts/post/content
) 我发现它仅用于post类型:
if ( \'post\' === get_post_type() ) :
echo \'<div class="entry-meta">\';
if ( is_single() ) :
twentyseventeen_posted_on();
else :
echo twentyseventeen_time_link();
twentyseventeen_edit_link();
endif;
echo \'</div><!-- .entry-meta -->\';
endif;
所以,我不明白用什么模板来显示我的自定义帖子类型?与此相关,我有一个问题:
a) 如何“强制”主循环对自定义帖子类型使用与帖子类型相同的模板和/或
b) 当主页上的主循环显示时,如何将自定义模板分配给我的自定义帖子类型?
UPDATE
可以
this 如何使用?
function get_template_part_custom($content_name) {
global $post;
//its paper type?
if ($post->post_type == \'paper\') {
//get template-parts/content-paper.php
get_template_part( \'template-parts/content\', $content_name );
}else{
//fallback to the default
get_template_part( \'template-parts/content\', get_post_format($post) );
}
}