代码之家  ›  专栏  ›  技术社区  ›  Lukerb

按类别名称检索wp\U术语

  •  0
  • Lukerb  · 技术社区  · 8 年前

    我试图限制检索到的证明(使用 get_terms category_name=“xxx”似乎什么都没做,所以我不知所措。

    function testimonial_shortcode( $atts ) {
    
        $cat = $atts['cat'];
    
            $testim='<div id="owl-demo" class="owl-carousel owl-theme">';    
            $terms = get_terms( array(
                'taxonomy' => 'testimonial',
                'category_name' => $cat,
                'hide_empty' => true,
                ) );
            foreach($terms as $custom_texonomy){
                $imageid=get_option("testimonial_".$custom_texonomy->term_id."_testimonials__image");
    
                $imgurl=wp_get_attachment_image_src( $imageid, 'full');
    
                $testim.=' <div class="item">
        ...
    
        }
        add_shortcode( 'testimonialcat', 'testimonial_shortcode' );
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   FluffyKitten    8 年前

    “category\u name”不是get\u术语的有效参数。 这个 WP reference documentation here

    '名称'

    因此,假设要搜索的名称是$cat,请尝试将get\u terms参数更改为:

    $terms = get_terms( array(
            'taxonomy' => 'testimonial',
            'name' => $cat,
            'hide_empty' => true,
            ) );
    

    更新:

    get_terms 仅返回有关 您按照原始问题搜索。

    获取所有 帖子 get_posts tax_query 具体如下:

        $myposts = get_posts(array(
            'showposts' => -1, // get all posts
            'post_type' => 'post', // change to whatever post type you want, or leave out if you want to get all post types
            'tax_query' => array( 
                              array( 
                                'taxonomy' => 'testimonial', 
                                'field' => 'name', 
                                'terms' => $cat
                             ))
        ));
    

    get_posts documentation in WP Codex

        2
  •  0
  •   Rich    8 年前

    function testimonial_shortcode( $atts ) {
    
        $cat = $atts['cat'];
    
                $testim='<div id="owl-demo" class="owl-carousel owl-theme">';    
            $terms = get_terms( array(
                'taxonomy' => 'testimonial',
                'name' => $cat,
                'hide_empty' => true,
                ) );
            foreach($terms as $custom_texonomy){
                $imageid=get_option("testimonial_".$custom_texonomy->term_id."_testimonials__image");
    
                $imgurl=wp_get_attachment_image_src( $imageid, 'full');
    
                $testim.=' <div class="item">
        ...
    
        }
        add_shortcode( 'testimonialcat', 'testimonial_shortcode' );