在wordpress的类别列表中隐藏特定帖子


Hide specific posts from category list in wordpress

我在显示Wordpress类别中的帖子列表时遇到了一些问题,该类别将使用高级自定义字段排除基于自定义字段的一定数量的帖子。

这是我目前使用的很好地隐藏它的代码:

while ( have_posts() ) : the_post();
    $is_taken = get_field('taken_check', $this_id);
    if ($is_taken!=1) { 
        get_template_part( 'basket_selection' );
    } 
endwhile;

然而,它只是简单地隐藏帖子,但仍然将其视为"posts_per_page"函数上的帖子。

例如,总共有20个帖子,我将限制为每页10个帖子。如果我用上面的代码隐藏了3个帖子,它将只在第1页显示7个帖子,在第2页显示10个帖子。

有没有一种方法可以简单地忽略隐藏的帖子,而不将其视为"帖子"?

试试这个:在get_post查询本身中应用自定义字段参数。

$posts = get_posts(array(
    'posts_per_page' => 10,
    'post_type' => '<YOUR_POST_TYP>',
    'meta_key' => 'taken_check',
    'meta_value' => '<DEFAULT_VALUE_OF_taken_check>'
));

这里有很多值得阅读的内容:http://codex.wordpress.org/Template_Tags/get_posts

我已经设法通过在category.php中将get_posts更改为wp_query来解决这个问题。

我首先添加了这个代码来检测查看的当前类别,并过滤查询以仅显示taken_check=0。

    $this_cat = get_category(get_query_var('cat'), 'ARRAY_A', false);
    foreach ($this_cat as $this_cat){
        $this_catid = $this_cat;
        break;
    }
    $args = array(
            'posts_per_page' => 10,
            'post_type' => 'post',
        'cat' => $this_catid,
        'orderby' => 'title',
        'order' => 'ASC',
        'paged' => $paged,
        'meta_query' => array(
            array(
                'key' => 'taken_check',
                'value' => '0',
            )
        )
     );
$wp_query = new WP_Query($args);

然后我继续使用默认的循环序列。唯一奇怪的代码是不必要的foreach循环,它根据当前页面而不是帖子来检测当前类别。仍然困惑于为什么我不能只使用$this_cat[0],因为它是一个数组。它不断返回空白。

哦,好吧,但它现在可以分页了,所以我很高兴:)谢谢你的帮助!