我正在尝试创建基于帖子标题的导航。顶部的主要导航是类别,侧边栏中将是相关类别的帖子标题。
我使用的代码可以工作到一定程度(我从按与页面标题匹配的类别列出 WordPress 帖子得到它)。
但是我有两个问题。
主要问题是,当您访问单个帖子时,它仅在侧边栏中列出该帖子,而没有同一类别的其他帖子。
第二个小问题,是它在我不需要的时候列出页面。
<p>below lists the right posts for the right
cats but fails at the single post stage</p>
<?php
$test = get_the_title();
$args = array( 'cat_name' => $test );
$args = array_merge( $args , $wp_query->query );
get_posts( $args ); while (have_posts()) { the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<ul>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
</ul>
<?php } ?>
如有任何帮助,我们将不胜感激。谢谢
对于第二个问题,您可以添加一个参数 'post_type' => 'post' 它只会显示帖子。
$test = get_the_title();
$args = array( 'cat_name' => $test,'post_type' => 'post' );
$args = array_merge( $args , $wp_query->query );