我使用WP_Query来获取同一类别中的相关帖子,但是当显示为html时,类别名称出现在帖子的顶部。 如何通过将参数设置为 WP_Query 而不使用 CSS 来删除它们?
<ul class="post-categories">
<li>...</li>
<li>...</li>
</ul>
.PHP:
<?php
$relateds = new WP_Query(array('post_type' => 'post',
'posts_per_page' => 10, 'cat' => the_category(), 'post__not_in'
=> array(get_the_ID())));
if ( $relateds->have_posts() ):
while ( $relateds->have_posts() ): $relateds->the_post(); ?>
<a href="<?php the_permalink() ?>" class="media">
<div class="d-flex align-self-start">
<?php the_post_thumbnail(); ?>
</div>
<div class="media-body pl-3">
<div class="media-title">
<?php echo wp_trim_words(get_the_title(), 15); ?>
</div>
</div>
</a>
<?php endwhile; else: endif;
wp_reset_postdata(); ?>
</aside><!-- /.blog-sidebar -->
<?php endwhile; ?>
当前结果是这样的:
类别 1
类别 2
帖子 1
帖子 2
帖子 3
。
但我只想要:
Post 1
Post 2
Post 3
要删除类别,只需删除变量$relateds的函数the_category((,因此要执行此操作, 改变:
$relateds = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 10, 'cat' => the_category(), 'post__not_in' => array(get_the_ID())));
自:
$relateds = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 10, 'post__not_in' => array(get_the_ID())));
希望这对您有所帮助!