【问题标题】:WP_QUERY, ARRAYS, Duplicate post if has multiple categoriesWP_QUERY,ARRAYS,如果有多个类别,则重复帖子
【发布时间】:2021-12-24 01:03:05
【问题描述】:

我有一个带有 wp_query 循环的短代码,它输出一个类别列表作为导航和帖子列表。原始代码要复杂得多,因此我尝试删除不必要的行以便更好地理解。

function filter_shortcode($atts, $content = null) {

    global $post;
    
    $attributes = shortcode_atts(
        array(
            'type'      => '',  
            'num'       => -1
         ), $atts);

    $args = array(
        'post_type'      => $attributes["type"],
        'posts_per_page' => $attributes["num"]
    );  
        
    $post_query = new WP_Query($args);
    
    $master_array = array();
    
    if ($post_query->have_posts() ) {
            
        while ($post_query->have_posts()) {
                
            $post_query->the_post();
                    
            $nav_arr = wp_get_post_terms( get_the_ID(), 'solutions_category' );
            
            // Problem here
            foreach($nav_arr as $nav_value ) {
                $nav = $nav_value->name; 
            }
            
            
            $box_tooltip = '<div>Lorem Ipsum</div>';
            $info_box_title = '<h5 style="margin-bottom: 0;">' . strtoupper($post->post_title) . '</h5>';       
                
                
            if (!($master_array[$nav])) {
                $master_array[$nav] = array();
                array_push(
                      $master_array[$nav],
                      [ 
                        'icon_title'  => $info_box_title, 
                        'tooltip'     => $box_tooltip,  
                      ]
                );
            } else {
                array_push(
                      $master_array[$nav],
                      [ 
                        'icon_title'  => $info_box_title, 
                        'tooltip'     => $box_tooltip,  
                      ]
                );
            }
                
        } // End of while have_posts
        
        wp_reset_postdata();

        /* Foreach master array */
        foreach ((array)$master_array as $master_key => $master_value) {
            
            /* Navigation */
            $navigation .= '<div>'. $master_key .'</div>';

            foreach($master_value as $query_key => $query_value) {
                
                $output_inner = '';     
                
                foreach ($query_value as $object_key => $object_value) {
                    $output_inner .= $object_value;
                }
                /* Content */
                $output_outer .= '<div class="'. $master_key .'">'. $output_inner .'</div>';
                
            } // end foreach mastervalue

        } // end foreach master
    
    } // end if have posts
    
    return '
            <div style="margin-bottom: 30px;">'. $navigation .'</div>
            <div>'. $output_outer .'</div>
           ';
            
}
add_shortcode('cpt_filter', 'filter_shortcode');

输出:

问题是,如果帖子有多个类别,该函数将只输出一次帖子。例如,“GOOGLE WORKSPACE”有两个类别——“协作”和“交流”,但它只输出一次。

如何输出“GOOGLE WORKSPACE”与其拥有的类别数量一样多,并指定其自己的类别类?

var_dump($nav_arr) 在“GOOGLE WORKSPACE”迭代后:array(2) { [0]=&gt; object(WP_Term)#3992 (10) { ["term_id"]=&gt; int(825) ["name"]=&gt; string(13) "Collaboration" ["slug"]=&gt; string(13) "collaboration" ["term_group"]=&gt; int(0) ["term_taxonomy_id"]=&gt; int(825) ["taxonomy"]=&gt; string(18) "solutions_category" ["description"]=&gt; string(0) "" ["parent"]=&gt; int(0) ["count"]=&gt; int(8) ["filter"]=&gt; string(3) "raw" } [1]=&gt; object(WP_Term)#3986 (10) { ["term_id"]=&gt; int(824) ["name"]=&gt; string(13) "Communication" ["slug"]=&gt; string(13) "communication" ["term_group"]=&gt; int(0) ["term_taxonomy_id"]=&gt; int(824) ["taxonomy"]=&gt; string(18) "solutions_category" ["description"]=&gt; string(0) "" ["parent"]=&gt; int(0) ["count"]=&gt; int(8) ["filter"]=&gt; string(3) "raw" } }

尝试检查 $nav_arr 是否有多个值 if (sizeof($nav_arr) &gt; 1) { .... } 并创建单独的数组,但卡住了。

【问题讨论】:

    标签: php arrays wordpress shortcode


    【解决方案1】:

    问题似乎出在以下 foreach 语句中:

    foreach($nav_arr as $nav_value ) {
        $nav = $nav_value->name; 
    }
    

    您正在遍历数组一次,并将 $nav 的值设置为“协作”,然后当您第二次遍历它时(因为 $nav_arr 中有两个项目),您只是在重写 $ nav 到“Communication”的新值,最后只有一个 $nav。

    $nav 应该保存为值数组,而不是单个值的字符串,如下所示:

    foreach($nav_arr as $nav_value){
        $nav[] = $nav_value;
    }
    

    没有看到完整的代码,我很难理解发生了什么......但我认为你可以替换所有

    if (!($master_array[$nav])) {
                $master_array[$nav] = array();
                array_push(
                      $master_array[$nav],
                      [ 
                        'icon_title'  => $info_box_title, 
                        'tooltip'     => $box_tooltip,  
                      ]
                );
            } else {
                array_push(
                      $master_array[$nav],
                      [ 
                        'icon_title'  => $info_box_title, 
                        'tooltip'     => $box_tooltip,  
                      ]
                );
            }
    }
    

    以下内容

    foreach($nav as $navitem){
        $master_array[$nav] = array(
            'icon_title'  => $info_box_title,
            'tool_tip'    => $box_tooltip
        ),
    }
    

    【讨论】:

    • foreach 循环之前创建了一个空数组,因此它将在每次迭代时从一个空数组开始 - $nav = []; 现在 var_dump($nav) 用于“GOOGLE WORKSPACE”是:array(2) { [0]=&gt; string(13) "Collaboration" [1]=&gt; string(13) "Communication" } 我应该改变什么在array_pushforeach ($master_array ...)foreach($master_value ...)foreach ($query_value ...)?
    猜你喜欢
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    相关资源
    最近更新 更多