php
  • wordpress
  • shortcode
  • 2017-08-17 1 views 0 likes 
    0

    検索した賞賛(get_termsを使用)をカテゴリ名で限定しようとしています。 category_name = "xxx"は何もしていないようですので、私は迷っています。カテゴリ名でwp_termsを取得

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

    答えて

    0

    'category_name'はget_termsの有効な引数ではありません。 WP reference documentation here には、受け入れ可能な引数がリストされています。あなたが望むものは:

    '名前':(文字列|配列)オプションです。用語を返す名前または名前の配列。

    ので、$猫を仮定すると、検索したい名前である、にご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 
             )) 
        )); 
    

    参考:get_posts documentation in WP Codex

    +0

    この種の作品。基本的に、私が今見ているのは、そのカテゴリの最初の投稿です。そして、ループは止まります。それがどんなアイデアなのか? – Lukerb

    +0

    名前を1つだけ検索すると、1つの結果しか期待できません。 'get_terms'はあなたが検索した用語の詳細を返します。あなたはあなたの分類法にその名前の用語が1つしかないと仮定します。その言葉に関連する投稿を取得しようとしていましたか? – FluffyKitten

    +0

    ええと...カスタム投稿タイプのカテゴリからすべてのものをループしようとしています...どんな提案ですか? – Lukerb

    0

    カテゴリ名は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'); 
    
    関連する問題