我正在使用模板系统的示例代码。
此地址的页码:http://project.test/my_cpt/hello-post/
.
无法理解原因is_singular( \'my_cpt\' )
是true
虽然in_the_loop()
是false
.
在页面模板中The Loop "E;“工程”:
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
?>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php
}
}
我想我的问题是什么时候is_singular() && in_the_loop()
都是真的?当我跑的时候if ( have_posts() ) { while ( have_posts()...
这是循环中的还是创建循环?
更新部分与执行if &&
测验
上面的示例用于template_include
hook正在运行页面、帖子,甚至链接、菜单、媒体等。这就是为什么这两个测试都是必要的。
过滤器如下所示:
add_filter( \'template_include\', array( __CLASS__, \'my_template_include_method\' ) );
add_filter( \'template_include\', \'my_template_include_function\' );
public static function my_template_include_function( $original_template ) {
if ( is_singular( \'my_cpt\' ) && in_the_loop() ) {
return wpbp_get_template_part( MMC_TEXTDOMAIN, \'content\', \'my_template\', false );
}
return $original_template;
}
Thewpbp_get_template_part
来自我最近发现的一个插件样板Wordpress Plugin Boiler Plate.由于目前我正在寻找一个单数帖子,我可以single_template
像这样:
add_filter( \'single_template\', array( __CLASS__, \'my_single_include_function\' ) );
public static function my_single_include_function( $single_template ) {
global $post;
if ( \'my_cpt\' === $post->post_type ) {
return wpbp_get_template_part( MMC_TEXTDOMAIN, \'single\', \'my_template\', false );
}
return $single_template;
}
模板本身看起来像:templates/content-my_template
和templates/single-my_template
.