dongzouhe9734 2017-10-05 21:34
浏览 67
已采纳

在wordpress中通过标签检索和显示图像

i iam creating a section with tags with this code, is a function to retrieve tags and exclude some tags also,

$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>

then i put this code in my functions.php file in wordpress, to make avaliable the tag box in the picture managment, so i can tag pictures now,

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

so my question is how can find and display the images by the id tag ? sorry my bad english, is not my native lenguage. any help is very welcome. thanks

  • 写回答

1条回答 默认 最新

  • duanming0494 2017-10-05 22:13
    关注

    You can actually handle this with a basic WP_Query call. There's lots of details and options for the WP_Query object here, but I'd think you could do something like this:

    $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 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计