首先,您有一个语法错误:
$page_title = the_title();
The
the_title()
模板标记将回显帖子标题,而不是返回帖子标题。您将要使用
get_the_title()
而是:
$page_title = get_the_title();
其次,只需将当前帖子从二次查询中排除,通过传递
the post__not_in
post parameter 到您的
WP_Query()
呼叫
更改此项:
$args = array( \'post_type\' => \'recipe\', \'tag\' => $tags );
。。。对此:
$args = array(
\'post_type\' => \'recipe\',
\'tag\' => $tags,
\'post__not_in\' => array( $post->ID )
);
然后,您就不需要在辅助查询循环中测试它了。
我会这样重写您的二次查询循环:
$related_posts_args = array(
\'post_type\' => \'recipe\',
\'tag\' => $tags,
\'post__not_in\' => array( $post->ID )
);
$related_posts = new WP_Query( $related_posts_args );
if ( $related_posts->have_posts() ) :
?>
<ul>
<?php
while ( $related_posts->have_posts() ) : $related_posts->the_post();
?>
<li><a href="<? the_permalink(); ?>"><? the_title(); ?></a></li>
<?php
endwhile;
?>
</ul>
<?php
endif;
wp_reset_postdata();