简体   繁体   English

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

[英]Hide specific posts from category list in wordpress

I'm having some trouble displaying a list of posts from a Wordpress Category that will exclude a certain number of post based on a custom field using Advance Custom Fields. 我在显示Wordpress类别的帖子列表时遇到一些麻烦,该列表将基于使用“高级自定义字段”的自定义字段排除一定数量的帖子。

Here's the current code I'm using that hides it nicely: 这是我正在使用的当前代码,可以很好地隐藏它:

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

However, it simply just hides the post but still considers it as a post on the "posts_per_page" function. 但是,它只是隐藏帖子,但仍将其视为“ posts_per_page”函数上的帖子。

For example, There are 20 posts in total and I've set the limit to 10 posts per page. 例如,总共有20个帖子,我已将限制设置为每页10个帖子。 If I hide 3 posts with the code above, it will only display 7 posts in page 1 and 10 posts in page 2. 如果我用上述代码隐藏3个帖子,它将仅在第1页显示7个帖子,在第2页显示10个帖子。

Is there a way to simply just ignore the hidden posts and not count it as a "post"? 有没有一种方法可以简单地忽略隐藏的帖子,而不将其视为“帖子”?

Try this: Apply Custom Fields Parameters in get_post query itself. 尝试以下操作:在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>'
));

Lots to read here: http://codex.wordpress.org/Template_Tags/get_posts 很多阅读这里: http : //codex.wordpress.org/Template_Tags/get_posts

I've managed to solve it by changing the get_posts to wp_query within the category.php. 我设法通过将category.php中的get_posts更改为wp_query来解决它。

I first added this code to detect the current category viewed and filter the query to only display taken_check = 0. 我首先添加了此代码以检测当前查看的类别并过滤查询以仅显示taked_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);

I then just continued with the default loop sequence. 然后,我继续默认的循环顺序。 The only weird code is the unnecessary foreach loop to detect the current category based on the current page and not from a post. 唯一奇怪的代码是不必要的foreach循环,该循环无需根据帖子而是根据当前页面来检测当前类别。 Still puzzled as to why I can't just use $this_cat[0] since it's an array. 对于为什么我不能只使用$ this_cat [0]仍然感到困惑,因为它是一个数组。 It keep returning blank. 它保持空白。

Oh well, but it works now with pagination, so I'm happy :) Thanks for all the help! 哦,很好,但是现在可以分页使用了,所以我很高兴:)感谢所有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM