按类别显示相关帖子,但忽略一个类别

时间:2017-02-27 作者:Austin

我想在我的博客底部显示4篇相关帖子(没有插件)。但是,我想排除某个类别。

例如,如果我的博客文章属于类别23, 我想忽略类别2 并且只查找类别为的博客帖子3. 以下是single.php:

Note: 我下面的代码当前不工作

$related = get_posts( array(
    \'category__in\'  => wp_get_post_categories( $post->ID ),
    \'numberposts\'   => 4,
    \'orderby\'       => \'date\',
    \'post__not_in\'  => array( $post->ID ),
    \'cat\'           => \'-2\'
) );

if( $related ) { 
    foreach( $related as $post ) {
        setup_postdata($post);
         /** .. **/
    }
}
Update: 类别2 是如此普遍,我想在搜索中忽略它,但不想隐藏这些结果。

例如,此帖子属于类别23. 我想找到其他类别的帖子3, 也许他们有分类2, 但我只想按类别搜索3.

Update 2: 我有以下代码,我相信它现在工作正常:

$cat_ids = get_the_category();
if( ! empty( $cat_ids ) ) {
    $post_cat_ids = array();
    foreach( $cat_ids as $cat_id ) {
        if( $cat_id->cat_ID != 2 ) {
            $post_cat_ids[] = $cat_id->cat_ID;
        }
    }
}

$related = get_posts( array(
    \'category__in\' => wp_get_post_categories( $post->ID ),
    \'numberposts\' => 4,
    \'orderby\' => \'date\',
    \'exclude\' => array( $post->ID ),
    \'category\' => $post_cat_ids
    ) );

5 个回复
最合适的回答,由SO网友:Fayaz 整理而成

我可以从你的问题中得出以下结论:

要忽略相关post查询中的一个类别(可能更多)。

但是,您不想实际将这些帖子从该类别中排除(如果任何帖子属于该类别,但也属于您要查找的其他类别)。

根据上述假设,您可以使用以下代码(注释中的代码中给出了一些解释):

    // set the category ID (or multiple category IDs)
    // you want to ignore in the following array
    $cats_to_ignore = array( 2 );
    $categories = wp_get_post_categories( get_the_ID() );
    $category_in = array_diff( $categories, $cats_to_ignore );
    // ignore only if we have any category left after ignoring
    if( count( $category_in ) == 0 ) {
        $category_in = $categories;
    }
    $cat_args = array(
        \'category__in\'   => $category_in,
        \'posts_per_page\' => 4,
        \'orderby\'        => \'date\',
        \'post__not_in\'   => array( get_the_ID() )
        );
    $cat_query = new WP_Query( $cat_args );
    while ( $cat_query->have_posts() ) : $cat_query->the_post();
        /* just example markup for related posts */
        echo \'<h2><a href="\' . get_the_permalink() . \'">\' . get_the_title() . \'</a></h2>\';
    endwhile;
    // reset $post after custom loop ends (if you need the main loop after this point)
    wp_reset_postdata();
您不能使用\'cat\' => \'-2\'\'category__not_in\' => array(2) 因为这将排除所有具有类别的帖子2, 即使这些帖子也有其他类别。因此,我没有排除,而是忽略了类别2 在使用此代码的查询中:array_diff( $categories, $cats_to_ignore );.

Note: 我用过WP_Query 而不是get_posts() 因为迭代WP_Query 看起来更像原始循环。但你当然可以使用get_posts() 函数以及它调用的WP_Query 无论如何,在内部。

SO网友:Rahul

试一试category__not_in 参数:

$related = get_posts( array(
    \'numberposts\'   => 4,
    \'orderby\'       => \'date\',
    \'category__in\'  => wp_get_post_categories( $post->ID ),
    \'category__not_in\' => array(2);
) );
它应该会起作用。

参考号:https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

SO网友:Abdus Salam
function custom_related_post()
{
    global $post;
    $related = get_posts(array(\'category__in\' => wp_get_post_categories($post->ID), \'numberposts\' => 2, \'post__not_in\' => array($post->ID)));
    ?>
    <div class="related-post-content wow fadeInUp">
        <h5><?php esc_html_e(\'Related Posts\', \'text-domain\'); ?></h5>
        <div class="related-post d-flex">
            <?php
            if ($related) foreach ($related as $post) {
                setup_postdata($post);
                /* translators: used between list items, there is a space after the comma */

                ?>
                <div class="single-relatd-post">
                    <div class="img-overflow">
                        <a href="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a>
                    </div>
                    <h6><?php the_title(); ?></h6>
                    <p><?php the_author(); ?> <span>-</span> <?php echo get_the_date(); ?></p>
                </div>
                <?php wp_reset_postdata();
            }
            ?>
        </div>
    </div>

<?php }
SO网友:Shubham Ingale

简单使用

\'category__not_in\' => array( category id )

从特定类别中排除帖子。

SO网友:Austin
$cat_ids = get_the_category();

if( ! empty( $cat_ids ) ) {
    $post_cat_ids = array();

    foreach( $cat_ids as $cat_id ) {
        if( 2 != $cat_id->cat_ID ) {
            $post_cat_ids[] = $cat_id->cat_ID;
        }
    }
}

$related = get_posts( array(
    \'category__in\'  => wp_get_post_categories( $post->ID ),
    \'numberposts\'   => 4,
    \'orderby\'       => \'date\',
    \'exclude\'       => array( $post->ID ),
    \'category\'      => $post_cat_ids
) );