1

我在尝试获取自定义分类法中的类别时遇到问题

分类=投资组合类别

我通过这个插件创建了 5 个新类别:https ://wordpress.org/plugins/hero-portfolio/

5 个新类别,我们以 catA、catB...、catE 为例

我想获取这些类别的名称(catA 等),以便将其用作过滤器函数的类。当我尝试通过各种方法显示值时,根本没有输出。这些是我尝试过的:

发现

  • $category_slug
  • $期限
  • $term_id
  • $id

我也试过

  • get_category_by_slug('category-slug')
  • get_term_by('slug', $value, $taxonomy )

这是代码:

    if($category_slug!=''){                                  
         $term = term_exists($category_slug);
         if ($term == 0 || $term == null) {
            echo "The ".$category_slug." does not exist!";
            return;
         }
         $term_id = $term;
         if(is_array($term)){
            $term_id = $term['term_id'];
         }
        $args = array(
            'tax_query' => array(
                array(
                    'taxonomy' => 'portfolio_categories',
                    'field' => 'id',
                    'terms' => $term_id
                )
            )
        );
        $rx_query = array_merge($rx_query, $args);                  
    }               

    $labels = PluginOptions::getInstance()->getLabels();        

    $out = '<div class="rx_hero_portfolio container-fluid">';           
        $query = new WP_Query($rx_query);
        if($query->have_posts()) {
            $groupCount = -1;

            while($query->have_posts()){
                $groupCount++;
                if($groupCount==0){
                    $out .= '<div class="row">';                        
                    //echo $groupCount." -open row <br />";
                }

                $query->the_post();
                $id = get_the_ID();
                $post_options = new RxPostOptions($id);
                $thumbnail_url = $post_options->getFeaturedImage($id, 800, 650);                    
                $title = get_the_title($id);
                $date = get_the_date(Y);                    
                $term = get_term( $id, $taxonomy );

                $out .= '

                <div class="col-md-4 hero_thumb_ui '. $term .'">
                    <div class="hero_thumb_container"><a href="'.get_permalink($id).'" class="hero_thumb_image_link"><img src="'.$thumbnail_url.'" alt="" /></a></div>
                    <div class="rx_hero_hoverui">
                        <h3 class="rx_hero_thumb_title">'.$title.'</h3>
                        <p class="rx_hero_excerpt">'.$post_options->get_the_excerpt_max_charlength($post_options->getExcerptThreeCols()).'</p>
                        <p class="rx_hero_excerpt">'. $date .'</p>
                        <div class="read_more_ui_bottom">
                            <a class="rx_hero_read_more" href="'.get_permalink($id).'">'.$labels['readMoreLB'].'</a>
                        </div>                                                      
                    </div>                      
                </div>
                ';      


                if($groupCount==2){                     
                    //echo $groupCount." -close row <br />";
                    $out .= '</div>';
                    $groupCount = -1;
                }                    
            }
            if($groupCount==0 || $groupCount==1){
                //echo $groupCount." -close row final <br />";
                $out .= '</div>';
            }               
        } else {
            // no posts found
            //echo "no posts found";
        }
        /* Restore original Post Data */
        wp_reset_postdata();
    $out .= '</div>';
    return $out;        
}

这是代码的另一部分,我认为这可能会给我们一个线索,脚本如何根据指定的自定义类别(catA 等)找到相关帖子

<!--related projects-->
                <div class="rx_related_projects">
                <?php
                $showRelatedPosts = false;
                if($showRelated){

                    $terms = get_the_terms($post->ID , 'portfolio_categories', 'string');
                    if(is_array($terms)){

                        $term_ids = wp_list_pluck($terms,'term_id');
                        if(is_array($term_ids)){
                            $comma_separated_ids = implode(",", $term_ids);
                            $ids = explode(',', $comma_separated_ids);                      
                        }


                        if(isset($ids)){
                                $posts_array = get_posts(array('numberposts'=>$relatedProjectsMaxNo, 'post_type'=>RX_PORTFOLIO_SLUG, 'post_status'=>'publish',
                                'exclude'=>get_the_ID(), 'tax_query'=>array(
                                        'relation'=>'AND',
                                        array('taxonomy'=>'portfolio_categories',
                                        'field'=>'id',
                                        'terms'=>$ids                                   
                                        )
                                    )
                                ));                                                 
                        }                                       
                        if(is_array($posts_array)){                             
                            if(sizeof($posts_array)>0){
                                $showRelatedPosts = true;
                            }
                        }

                    }
                }                               
                ?>
                <?php if($showRelatedPosts):?>
                    <h2 class="rx_related_projects_title"><?php echo $labels['relatedLB'];?></h2>
                    <div class="rx_related_underline"></div>    
                                <?php
                                require_once(HR_CLASS_PATH.'/com/riaextended/php/libs/rx__resizer.php');
                                global $post;
                                $out = '';
                                $groupCount = -1;               
                                foreach($posts_array as $post ) :   setup_postdata($post); ?>                                                                                                   
                                <?php
                                    $groupCount++;
                                    if($groupCount==0){
                                        $out .= '<div class="row">';                        
                                        //echo $groupCount." -open row <br />";
                                    }                                                                   

                                    $p_thumb = $post_options->getFeaturedImage($post->ID, 800, 500);                                    
                                    $permalink = get_permalink($post->ID);
                                    $r_title = get_the_title($post->ID);
                                    $out .= '
                                    <div class="col-md-4">
                                        <div class="rx_related_project">
                                            <a href="'.$permalink.'" class="hero_thumb_image_link"><img src="'.$p_thumb.'" alt="" /></a>
                                            <div class="rx_related_overlay" data-url="'.$permalink.'">
                                                <div class="rx_related_cross"><img src="'.RX_TEMPPATH.'/images/cross.png'.'" alt="" /></div>
                                                <h3 class="rx_related_title">'.$r_title.'</h3>                                              
                                            </div>                                          
                                        </div>
                                    </div>
                                    ';
                                    if($groupCount==2){                     
                                        //echo $groupCount." -close row <br />";
                                        $out .= '</div>';
                                        $groupCount = -1;
                                    }                                                                   
                                ?>                                                              
                                <?php endforeach; ?>            
                                <?php
                                if($groupCount==0 || $groupCount==1){
                                    //echo $groupCount." -close row final <br />";
                                    $out .= '</div>';
                                }
                                echo $out;                              
                                ?>          
                <?php endif;?>              
                </div>              
                <!--related projects-->
4

0 回答 0