2017-10-05 61 views
1

我IAM创建与此代码的标签的部分,是检索标记和排除一些标签也是一个函数,检索和显示图像的标签在WordPress

$args = array('name__like' => $name_like, 'exclude' => array(75,177,42,74,197,36,40,140,162,108,86,47,4,29,22,215,87,151,104),'order' => 'ASC'); 
     $tags = get_tags($args); 


     if (!empty($tags) && !is_wp_error($tags)) { 
      $count = count($tags); 
      $i=0;?> 

      <ul class="my_term-archive"> 
      <?php 

       foreach ($tags as $tag) { 
       $i++; 

      $tag_link = get_tag_link($tag->term_id); 
      $tag_id = get_tag_ID($tag->name); 
      if(strtolower(substr($tag->name,0,1)) !=$name_like){ 
       continue; 
      } 
//i need a function here to retrieve images with the id of the tag 
//attached 


////// 

$html .= "<li><a href='{$tag_link}' id='{$tag_id}' title='{$tag->name} Tag' class='{$tag->slug}'>"; 
$html .= "{$tag->name}</a></li>"; 

       } 
     } 
     echo $html; 
     ?> 
     </ul> 

然后我把这个代码在我的功能。在WordPress PHP文件,使产品相关图片的图片同治标签盒,所以我现在可以标记图片,

function wptp_add_tags_to_attachments() { 
    register_taxonomy_for_object_type('post_tag', 'attachment'); 
} 
add_action('init' , 'wptp_add_tags_to_attachments'); 

所以我的问题是如何找到并显示由ID标记的图像? 抱歉,我的英语不好,不是我的母语。任何帮助是非常受欢迎的。谢谢

回答

0

你实际上可以用一个基本的WP_Query调用来处理这个。有很多details and options for the WP_Query object here,但我认为你可以做这样的事情:

$args = array(
    'post_type' => 'attachment', 
    'tax_query' => array(
     array(
      'taxonomy' => 'post_tag', 
      'field' => 'slug', 
      'terms' => 'whatever-your-tag-slug-is', 
     ), 
    ), 
); 
$query = new WP_Query($args); 
+1

感谢指出我在正确的方向这个解决方案适用于我 – user3765369