【问题标题】:Exclude category from author posts list in Wordpress从 Wordpress 中的作者帖子列表中排除类别
【发布时间】:2017-01-08 11:33:35
【问题描述】:

我正在寻找一种从作者页面中排除类别的方法。我有一个作者撰写的类别,但是当您导航到作者存档页面时,我希望该类别在显示该作者的所有其他帖子时不显示。

以下是构成我的自定义作者页面的代码

    <?php get_header(); ?>
<?php get_header(); ?>
<div class="mh-wrapper clearfix">
    <div id="main-content" class="mh-content">

    <?php
        mh_before_page_content();
        mh_magazine_lite_page_title();
        ?>

    <p><img style="border-bottom:3px solid black;" src="http://chaseonair.com/wp-content/themes/theme-andrews/images/chase-blog-header.jpg"></p>

    <?php
        if (category_description()) { ?>

        <?php
        }
        if (is_author()) {
            mh_magazine_lite_author_box();
        }
        if (have_posts()) {
            while (have_posts()) : the_post();
                get_template_part('content', 'loop');
            endwhile;
            mh_magazine_lite_pagination();
        } else {
            get_template_part('content', 'none');
        } ?>
    </div>
    <?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>

我在网上找了这个

query_posts($query_string . "&cat=-1");

我尝试过使用它,但是它要么会导致错误,要么当我导航到旧帖子时,它只会再次显示前 5 个帖子。

任何帮助将不胜感激。

【问题讨论】:

    标签: wordpress loops posts author


    【解决方案1】:

    使用 pre_get_posts 过滤器来实现:

    <?php
        add_action( 'pre_get_posts', 'exclude_category_from_author_pages' );
    
        function exclude_category_from_author_pages( $query ) {
            if( $query->is_main_query() && $query->is_author('jack') ) {
                $query->set( 'cat', '-4' );
            }
            else if( $query->is_main_query() && $query->is_author('john') ) { 
                $query->set( 'cat', '-5' );
            }
            else if( $query->is_main_query() && $query->is_author('steve') ) {  
                $query->set( 'cat', '-7' );
            }   
        }
    ?>
    

    请注意,您也可以使用$query-&gt;is_author(8),其中 8 是昵称为“john”的用户的用户 ID

    【讨论】:

    • 这对我不起作用。我应该把这段代码放在哪里?
    • 把它放在你的主题的functions.php的底部。只是不要忘记将“4”替换为您需要排除的实际类别的 ID。
    • 这就是问题所在。我有一位作者在 A 类和 B 类中发帖。我想从他们的作者档案页面中排除 B 类。然后我有另一位作者为 C、D、E 和 F 类写帖子,我也需要从该作者中排除 E 类。是否有我可以在我创建的自定义作者存档页面上使用的代码?我希望能够控制每个作者的类别排除。
    • 我已将此代码放在功能页面的顶部,但似乎没有任何效果。我要隐藏的类别仍然存在。
    • 我把代码放在我的functions.php文件的顶部。我暂时把它拿出来了。我的功能页面的代码位于chaseonair.com/html-pages/functions.txt。我确实用我自己的类别替换了 -4,用我的作者 ID 替换了 jack,但没有运气。我也为其他作者和类别做了这个。
    猜你喜欢
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多