【问题标题】:retrieving and display images by tag in wordpress在wordpress中按标签检索和显示图像
【发布时间】:2017-10-05 21:34:40
【问题描述】:

我正在用这段代码创建一个带有标签的部分,是一个检索标签并排除一些标签的功能,

$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的functions.php文件中,使图片管理中的标签框可用,所以我现在可以标记图片了,

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

所以我的问题是如何通过 id 标签查找和显示图像? 对不起,我的英语不好,不是我的母语。非常欢迎任何帮助。谢谢

【问题讨论】:

    标签: php wordpress image tags


    【解决方案1】:

    您实际上可以通过基本的 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 );
    

    【讨论】:

    • 感谢我指出正确的方向,这个解决方案对我有用
    猜你喜欢
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 2022-01-24
    相关资源
    最近更新 更多