【问题标题】:Have Wordpress posts that belong to multiple categories, exclude some categories from homepage拥有属于多个类别的 Wordpress 帖子,从主页中排除某些类别
【发布时间】:2014-08-10 20:02:50
【问题描述】:

所以我有类别 A、B、C 和 D。还有类别 1、2、3 和 4。我的帖子可以属于这四个类别中的任何一个。但我想从主页(并且仅从主页)中排除 B、4 和 D 类中的任何内容,即使它们也在其他类别中。

我一直在尝试插件和自定义代码并在谷歌上搜索了几个小时,但我就是无法让它工作。最后一批的尝试是对此的变体:

 <?php $query = new WP_Query( 'cat=-32,-99' ); ?>
 <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
 <?php if (! in_category ('-32')); ?>
 <?php x_get_view( 'ethos', 'content', get_post_format() ); ?>
 <?php endif; ?>
 <?php endwhile; 
 wp_reset_postdata();
 else : ?>
 <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
 <?php endif; ?>

欢迎提出任何建议。

我弄清楚了 pre_get_posts 应该如何处理来自其他地方的推荐。这就是我现在所拥有的,但它仍然无法正常工作...... gist.github.com/megantaylor/fb86fb9cfc026208b928

【问题讨论】:

    标签: php wordpress categories


    【解决方案1】:

    只看代码,这一行:

    if (! in_category ('-32'));

    看起来有点奇怪。

    首先,它看起来像是双重否定——它不属于第 32 类。

    另外,您可以将 id 作为字符串 '-32' 传递吗? – 文档建议 id 应该是一个整数。

    这有什么帮助吗?

    编辑:

    另外,您的if 应该以冒号结尾,所以:

    if(!in_category(32)):

    【讨论】:

    • 是的,在我玩这个的三个小时里,我开始踩自己的脚。呃……
    猜你喜欢
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多