【问题标题】:WP_Query - `category__not_in` not working when showing sticky postsWP_Query - `category__not_in` 在显示置顶帖子时不起作用
【发布时间】:2018-08-01 15:16:51
【问题描述】:

我正在尝试显示前 4 个不属于 videos 类别的置顶帖子,这是我当前的循环:

<?php $videos_cat_id = get_cat_ID('videos');

$args = array(
    'post__in' => get_option('sticky_posts'),
    'category__not_in' => $videos_cat_id,
    'ignore_sticky_posts' => 1,
    'posts_per_page' => 4,
);

$featured_loop = new WP_Query($args);

if ($featured_loop->have_posts()) : 
    while ($featured_loop->have_posts()) : $featured_loop->the_post(); ?>
        <div class="col-sm-6 m-bottom p-left-none p-right-none">
            <?php get_template_part('card-featured') ?>
        </div>
    <?php endwhile; wp_reset_postdata();
 endif; ?>

但我仍然看到呈现videos 类别的帖子。我不完全确定为什么这个循环不尊重category__not_in,有什么想法吗?

【问题讨论】:

    标签: php wordpress wordpress-theming custom-wordpress-pages


    【解决方案1】:

    试试这个:


    您可能需要在category__not_in 中提供一个数组值。而且由于get_cat_ID()函数返回字符串/整数,你可能需要这个->'category__not_in' =&gt; array($videos_cat_id)

    【讨论】:

    • 我在提交问题之前尝试了这种方法,但它仍然显示videos 类别的帖子。
    • 视频类别的 slug 是否正是 videos
    • 是的,拼写为videos(三重检查)
    • 我通过将featured 类别添加到具有videos 类别的所有帖子中,然后循环遍历这些暂时“解决”了它。不完全是我最初想要的,但确实成功了。
    猜你喜欢
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多