繁体   English   中英

通过category_name检索wp_terms

[英]Retrieve wp_terms by category_name

我正在尝试通过类别名称来限制检索到的推荐(使用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' );

“ category_name”不是get_terms的有效参数。 此处WP参考文档列出了可接受的参数。 您想要的是:

'name' :(string | array)可选。 要返回其术语的名称或名称数组。

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

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

更新:

按照原始问题, get_terms仅返回有关您搜索的术语的信息。

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

参考: WP Codex中的get_posts文档

category_name不是get_terms的有效参数,请尝试使用“名称”:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM