2012年6月4日月曜日

WordPressで指定したカテゴリーとタグに一致するメディアを表示する

Attachment Taxonomy Supportなどを使って、メディアにカテゴリーとタグをつけます。 そのあとで、galleryショートコードを拡張して、gallery tag="tag1"やgallery category="cat1"などと指定できるようにします。
            $tax_val = array();
            if ( !empty($tag) ) {
                $tax_val[] = array(
                                   'taxonomy' => 'attachment_tag',
                                   'field' => 'slug',
                                   'terms' => $tag
                                   );
            }
            if ( !empty($category) ) {
                $tax_val[] = array(
                                   'taxonomy' => 'attachment_category',
                                   'field' => 'slug',
                                   'terms' => $category
                                   );
            }
            $_attachments = get_posts( array(
                                            'post_type' => 'attachment',
                                            'post_status' => 'inherit',
                                            'posts_per_page' => -1,
                                            'tax_query' => $tax_val,
                                            'post_parent' => null
                                            ));
            $attachments = array();
            foreach ( $_attachments as $key => $val) {
  $attachments[$val->ID] = $_attachments[$key];
            }

0 件のコメント:

コメントを投稿