簡體   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