嗨,我有一个主要的类别是“动漫”,还有三个子类别是“正在进行中”-“完成”-“未来”(还有其他类别)。我正在使用此代码在上显示相关帖子single.php:
<div class="sixteen wide column recommendationss" style="display:none;padding-top: 0px;">
<div class="ui master table">
<div class="body two-cells">
<?php
$related = get_posts( array( \'category__in\' => wp_get_post_categories($post->ID), \'numberposts\' => 5, \'post__not_in\' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<div class="row">
<div class="cell cover">
<div class="ui lazy loaded" style="padding-bottom: 145.33%;">
<?php the_post_thumbnail();?>
</div>
</div>
<div class="cell info">
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<small><?php the_field(\'archivyear\'); ?></small>
</div>
</div>
<?php }
wp_reset_postdata(); ?>
</div>
</div>
</div>
那么,如何从这个查询中删除这四个类别呢?
最合适的回答,由SO网友:Syed Fakhar Abbas 整理而成
您可以使用category__not_in
参数以排除类别。
get_posts
使用WP_QUERY
参数
您的代码将是:
$related = get_posts( array(
\'category__in\' => wp_get_post_categories($post->ID),
\'numberposts\' => 5,
\'post__not_in\' => array($post->ID),
\'category__not_in\' => array(1,2,3,4)\'
) );