不包括某些类别的相关帖子

时间:2015-09-16 作者:Keefer

我正在使用一个相关帖子PHP WordPress查询,但在排除类别时遇到了问题。我试过排除viacatcategory__not_in 以及作为和不作为数组。我设置的错误是什么?

<?php $orig_post = $post;
    global $post;
    $categories = get_the_category($post->ID);
    if ($categories) {
    $category_ids = array();
    foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
    $args=array(
    \'cat\' => array(-6,-173,-174),
    \'category__in\' => $category_ids,
    \'category__not_in\' => (6, 173, 174),
    \'post__not_in\' => array($post->ID),
    \'posts_per_page\'=> 3, // Number of related posts that will be displayed.
    \'orderby\'=>\'rand\' // Randomize the posts
    );
    $my_query = new wp_query( $args );
    if( $my_query->have_posts() ) {
    echo \'<ul class="realted-article">\';
    while( $my_query->have_posts() ) {
    $my_query->the_post(); ?>
    <li>
     <a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
     </a>
     <div class="perfect-related_by-category">
        <strong><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></strong>
     <a href="<?php echo get_permalink();?>">   
        <?php if(get_field(\'summary_text\')==\'\')
            {echo substr(strip_tags(get_the_content()),0,90);}
            else{echo strip_tags(get_field(\'summary_text\'));}?>
     </a>
     </div>
    </li>
    <? }
    echo \'</ul>\';
    } }
    $post = $orig_post;
    wp_reset_query();
    ?>

1 个回复
SO网友:Pieter Goosen

您可以简单地运行foreach 循环并从返回的术语数组中排除术语。只是一些注释

  • cat 接受字符串或整数值,不接受and数组

  • category__not_in 应为数组

    这是我最近做的一个相关帖子功能的修改版本here. 请去看看,看看$args 应使用参数,请记住,它已移动到新函数中的第三个参数;-)

    下面是代码(我对代码进行了注释,因此您可以按照它进行操作,但它未经测试,需要PHP 5.4+)

    function get_related_posts( $taxonomy = \'\', $exclude = [], $args = [] )
    {
        /*
         * Before we do anything and waste unnecessary time and resources, first check if we are on a single post page
         * If not, bail early and return false
         */
        if ( !is_single() )
            return false;
    
        /*
         * Check if we have a valid taxonomy and also if the taxonomy exists to avoid bugs further down.
         * Return false if taxonomy is invalid or does not exist
         */
        if ( !$taxonomy ) 
            return false;
    
        $taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
        if ( !taxonomy_exists( $taxonomy ) )
            return false;
    
        /*
         * We have made it to here, so we should start getting our stuff togther. 
         * Get the current post object to start of
         */
        $current_post = get_queried_object();
    
        /*
         * Get the post terms
         */
        $terms = get_the_terms( $current_post->ID, $taxonomy );
    
        /*
         * Lets only continue if we actually have post terms and if we don\'t have an WP_Error object. If not, return false
         */
        if ( !$terms || is_wp_error( $terms ) )
            return false;
    
        // Define our array to hold the term id\'saveHTML
        $term_ids = [];
        foreach ( $terms as $term ) {
            // Check if we have terms to exclude and exclude them
            if ( $exclude ) {
                if ( in_array( $term->id, $exclude ) )
                    continue;
            } else {
                $term_ids[] = (int) $term->term_id;
            }
        /*
         * Set the default query arguments
         */
        $defaults = [
            \'post_type\' => $current_post->post_type,
            \'post__not_in\' => [$current_post->ID],
            \'tax_query\' => [
                [
                    \'taxonomy\' => $taxonomy,
                    \'terms\' => $term_ids,
                    \'include_children\' => false
                ],
            ],
        ];
    
        /*
         * Validate and merge the defaults with the user passed arguments
         */
        if ( is_array( $args ) ) {
            $args = wp_parse_args( $args, $defaults );
        } else {
            $args = $defaults;
        }
    
        return $q;
    }
    
    那么您可以按如下方式使用它

    if ( function_exists( \'get_related_posts\' ) ) {
        $related_posts = get_related_posts( 
            \'my_taxonomy_name\', // Taxonomy name, if build in, in should be \'category\'
            [1, 2, 3] // Array of terms to exclude
        );
        if ( $related_posts ) {
            foreach ( $related_posts as $post ) {
                setup_postdata( $post ); 
                // Use your template tags and html mark up as normal like
                the_title();
                the_content();
                // etc etc
            }
            wp_reset_postdata();
        }
    }
    

相关推荐

Post in multiple categories

我尝试在多个类别中列出帖子。管理面板中一切正常。我进入所有帖子,选择所需的帖子,然后进入编辑并选择一个类别。保存更改后,在“类别”选项卡下会写入旧类别和新类别。现在,当我访问网站并选择更新类别时,只有旧帖子,没有新帖子。例如:我有类别:电影、游戏、最佳和帖子:最佳电影、最佳游戏、最佳,我需要这样:最佳电影属于电影类别最佳游戏属于游戏类别,两者都属于最佳类别我使用日期和职位名称作为永久链接。