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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。