首页 > 解决方案 > 从相关帖子中删除具有特定类名的帖子 - WP

问题描述

我的一些帖子有一个类名“已禁用”,我必须从相关帖子中删除它们,因为它们有一个样式 display: none 所以它会插入它但不显示它。

如何过滤禁用类名的帖子并将它们添加到“posts__not_in”以避免它们?

<?php
$related = get_posts( array( 
    'numberposts' => 4, 
    'post__not_in' => array($post->ID) 
) );
     
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
     
       <a href="<?php the_permalink(); ?>" class="card <?php if( get_field('public') == 'no') echo 'disabled' ?>">
          <p><?php the_title();?></p>
       </a>
    
<?php } wp_reset_postdata(); ?>

标签: phpwordpress

解决方案


我的建议是创建一个类别并将其分配给帖子。然后使用 tax_query 从您的 get_posts() 中过滤掉这些帖子。下面的示例是伪代码:

$related = get_posts([ 
    'post_type' => 'post',
    'numberposts' => 4, 
    'post__not_in' => [$post->ID],
    'tax_query' => [
        [
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => 'DISABLED_CATEGORY_SLUG',
            'operator'  => 'NOT IN'
        ],
    ],
]);

推荐阅读