按category_name检索wp_terms

时间:2017-08-17 09:15:03

标签: php wordpress shortcode

我试图通过类别名称限制检索到的推荐(使用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 个答案:

答案 0 :(得分:0)

&#39; CATEGORY_NAME&#39;不是get_terms的有效参数。 WP reference documentation here get_posts documentation in WP Codex  列出可接受的参数。你想要的是:

&#39; name&#39; :( string | array)可选。要返回术语的名称或名称数组。

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

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

<强>更新

get_terms仅根据原始问题返回您搜索的 term 的相关信息。

要获取与字词相关联的所有帖子,您需要使用get_poststax_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
                         ))
    ));

参考:https://hyperledger.github.io/composer/tutorials/developer-guide.html

答案 1 :(得分:0)

category_name不是get_terms的有效参数,请尝试&#39; name&#39;代替:

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' );