Php 如何使用wordpress中的get_post()将get_template_part()用于特定帖子?
我正在创建我的第一个wordpress主题。 我目前正在使用循环和get_template_part()来显示我的帖子列表,如下所示:Php 如何使用wordpress中的get_post()将get_template_part()用于特定帖子?,php,wordpress,wordpress-theming,custom-wordpress-pages,Php,Wordpress,Wordpress Theming,Custom Wordpress Pages,我正在创建我的第一个wordpress主题。 我目前正在使用循环和get_template_part()来显示我的帖子列表,如下所示: while ( have_posts() ) { the_post(); get_template_part( 'content', get_post_format() ); } 这非常有效,正在加载content.php作为post的模板 我想在常规帖子输出的末尾使用get_template_part()显示特定帖子。 我在while循环后添
while ( have_posts() ) {
the_post();
get_template_part( 'content', get_post_format() );
}
这非常有效,正在加载content.php作为post的模板
我想在常规帖子输出的末尾使用get_template_part()显示特定帖子。
我在while循环后添加了这个用于显示Id为123的帖子:
get_post(123);
get_template_part( 'content', get_post_format() );
所以总代码是:
while ( have_posts() ) {
the_post();
if($post->ID != 123) // exclude Post 123 from output
get_template_part( 'content', get_post_format() );
}
get_post(123);
get_template_part( 'content', get_post_format() );
但这只是重复常规循环中的最后一个条目。
有人能帮忙吗
谢谢和问候
Jan以下是工作解决方案:
global $post;
$post = get_post(123);
setup_postdata($post);
get_template_part( 'content', get_post_format() );
很可能您可以使用
设置\u postdata解决您的问题
感谢开发人员-不幸的是,这不起作用。但我明白了:_post()生成一个对象,该对象自动存储在$post中;get_post仅返回该对象。因此,我的解决方案是$post=get_post(123),将项目存储在$post中,以便在get_模板部分中进一步使用。