我正在创建我的第一个 wordpress 主题。 我目前正在使用循环和 get_template_part() 来显示我的帖子列表,如下所示:
while ( have_posts() ) {
the_post();
get_template_part( 'content', get_post_format() );
}
这很好用,并且正在加载 content.php 作为帖子的模板。
我想在常规帖子输出的末尾使用 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() );
但这只是重复常规循环中的最后一个条目。 有人可以帮忙吗?
感谢和问候 一月
最佳答案
这是可行的解决方案:
global $post;
$post = get_post(123);
setup_postdata($post);
get_template_part( 'content', get_post_format() );
https://codex.wordpress.org/Function_Reference/setup_postdata
关于php - 如何在 wordpress 中使用 get_post() 将 get_template_part() 用于特定帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39101020/