我使用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
答案 0 :(得分:0)
要删除类别,只需删除变量 $ 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())));
希望这对您有所帮助!