http://naoyu.net/wordpress/tax_query/583/
ここに感謝
そもそもタクソノミーのタームを全て取得するコードが
あまりメジャーじゃないから悩んだ。
これでカスタムポストの全記事をターム別に一覧表示できる。
【備考】
アーカイブページで表現できるのはポストタイプのみで
タクソノミーページの全記事アーカイブはタクソノミー – タクソノミー名では
そのテンプレートが使われなかった。
つまりそんな表示はできない
/タクソノミー名/ではぺーじがありません。
/タクソノミー名/ラベル名/で一覧が表示されたようにみえたが
タクソノミーphpで表示されただけで ラベル名の部分は適当なもじでも同じように全部でた。
つまりないからタクソノミーページを表示してるだけだ。
つまり
固定ページに書いて表現する。
デフォルトで この表現であったら良いのに。
そのうちなるかな?
タクソノミーのタームを全て取得するコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $args = array ( 'parent' => 0, 'hierarchical' => 0, 'orderby' => 'term_order' , // Category Order and Taxonomy Terms Order を使用 'order' => 'ASC' ); $taxonomy_name = 'cate_jirei' ; $taxonomys = get_terms( $taxonomy_name , $args ); if (!is_wp_error( $taxonomys ) && count ( $taxonomys )): foreach ( $taxonomys as $taxonomy_term ): $url = get_term_link( $taxonomy_term ->slug, $taxonomy_name ); ?> <h3><div class = "jirei_title" ><i class = "icon-doc-inv" ></i> <a href= "<?php echo $url; ?>" > <?php echo $taxonomy_term ->name; ?> </a> </div> </h3> <?php endforeach ; endif ; ?> |
1 2 | $taxonomy_name = 'cate_jirei' ; $taxonomys = get_terms( $taxonomy_name , $args ); |
これだろう
あとは
フォーチでさらにクエリポストでまわした。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | <?php $args = array ( 'parent' => 0, 'hierarchical' => 0, 'orderby' => 'term_order' , // Category Order and Taxonomy Terms Order を使用 'order' => 'ASC' ); $taxonomy_name = 'cate_jirei' ; $taxonomys = get_terms( $taxonomy_name , $args ); if (!is_wp_error( $taxonomys ) && count ( $taxonomys )): foreach ( $taxonomys as $taxonomy_term ): $url = get_term_link( $taxonomy_term ->slug, $taxonomy_name ); ?> <h3><div class = "jirei_title" ><i class = "icon-doc-inv" ></i> <a href= "<?php echo $url; ?>" > <?php echo $taxonomy_term ->name; ?> </a> </div> </h3> <!----------------------BOX ALL-----------------------------> <div class = "jirei_box_all" > <?php $args = array ( 'posts_per_page' => -1, //'orderby' => 'menu_order', //'order' => 'ASC', 'tax_query' => array ( 'relation' => 'AND' , array ( 'taxonomy' => $taxonomy_name , 'field' => 'slug' , 'terms' => array ( $taxonomy_term ->slug ), //'operator'=>'NOT IN' ), ), //カテ例 'cat'=> 4, //カテ複数例 'cat'=> array(4,6) ); ?> <!----/宣言-----> <!----宣言2-----> <?php query_posts( $args ); ?> <!--★★★ループ基本--> <?php if ( have_posts () ) : while ( have_posts() ) : the_post(); ?> <div class = "jirei_box" > <div class = "jirei_box_img" style= "height:210px;" > <a href= "<?php the_permalink(); ?>" > <?php $title = get_the_title(); echo wp_get_attachment_image(get_post_meta( $post ->ID, "jirei_gazou" ,true),W210,0, array ( 'alt' => $title , 'title' => $title )); ?> </a> <?php //new $days =30; $today = date ( 'U' ); $entry =get_the_time( 'U' ); $diff1 = date ( 'U' ,( $today - $entry ))/86400; if ( $days > $diff1 ) { echo '<div class="new_icon"><img src="/img/new_icon/rainbow1.gif" width="31" height="12" alt="愛知春日井ホームページ制作NEW記事"></div>' ; } ?> </div> <h4><div class = "jirei_sub_title" ><i class = "icon-dot-circled" ></i> <a href= "<?php the_permalink(); ?>" ><?php the_title(); ?> </a></div></h4> </div> <?php endwhile ; endif ; ?> <?php wp_reset_query(); ?> <div style= "clear:both" ></div> </div class = "jirei_box_all" > <div style= "padding-top:30px;" ></div> <!----------------------BOX ALL-----------------------------> <?php endforeach ; endif ; ?> |
原文はこうだ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?php $args = array ( 'parent' => 0, 'hierarchical' => 0, 'orderby' => 'term_order' , // Category Order and Taxonomy Terms Order を使用 'order' => 'ASC' ); $taxonomy_name = 'item_category' ; $taxonomys = get_terms( $taxonomy_name , $args ); if (!is_wp_error( $taxonomys ) && count ( $taxonomys )): foreach ( $taxonomys as $taxonomy ): $url = get_term_link( $taxonomy ->slug, $taxonomy_name ); $tax_posts = get_posts( array ( 'post_type' => get_post_type(), 'posts_per_page' => 5, // 表示させたい記事数 'tax_query' => array ( array ( 'taxonomy' => 'item_category' , 'terms' => array ( $taxonomy ->slug ), 'field' => 'slug' , 'include_children' =>true, 'operator' => 'IN' ), 'relation' => 'AND' ) )); if ( $tax_posts ): ?> <h2 id= "<?php echo esc_html($taxonomy->slug); ?>" class = "" ><a href= "<?php echo $url; ?>" ><?php echo esc_html( $taxonomy ->name); ?></a></h2> <ul> <?php foreach ( $tax_posts as $tax_post ): ?> <li> <span class = "thumb" > <a href= "<?php echo get_permalink($tax_post->ID); ?>" > <?php if (has_post_thumbnail( $tax_post ->ID)) { echo get_the_post_thumbnail( $tax_post ->ID, 'post-thumbnail' ); } ?> </a> </span> <span class = "title" ><?php echo get_the_title( $tax_post ->ID); ?></span> </li> <?php endforeach ; ?> </ul> <?php endif ; endforeach ; endif ; ?> |
さらにタクソノミー シーズンで表現
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <?php $args = array ( 'parent' => 0, 'hierarchical' => 0, 'orderby' => 'term_order' , // Category Order and Taxonomy Terms Order を使用 'order' => 'ASC' ); $taxonomy_name = 'item_category' ; $taxonomys = get_terms( $taxonomy_name , $args ); if (!is_wp_error( $taxonomys ) && count ( $taxonomys )): foreach ( $taxonomys as $taxonomy ): $url = get_term_link( $taxonomy ->slug, $taxonomy_name ); $this_term = get_query_var( 'term' ); $tax_posts = get_posts( array ( 'post_type' => get_post_type(), 'posts_per_page' => 5, // 表示させたい記事数 'tax_query' => array ( array ( 'taxonomy' => 'item_category' , 'terms' => array ( $taxonomy ->slug ), 'field' => 'slug' , 'include_children' =>true, 'operator' => 'IN' ), array ( 'taxonomy' => 'season' , 'terms' => array ( $this_term ), 'field' => 'slug' , 'include_children' =>true, 'operator' => 'AND' ), 'relation' => 'AND' ) )); if ( $tax_posts ): ?> <h2 id= "<?php echo esc_html($taxonomy->slug); ?>" class = "" ><a href= "<?php echo $url; ?>" ><?php echo esc_html( $taxonomy ->name); ?></a></h2> <ul> <?php foreach ( $tax_posts as $tax_post ): ?> <li> <span class = "thumb" > <a href= "<?php echo get_permalink($tax_post->ID); ?>" > <?php if (has_post_thumbnail( $tax_post ->ID)) { echo get_the_post_thumbnail( $tax_post ->ID, 'post-thumbnail' ); } ?> </a> </span> <span class = "title" ><?php echo get_the_title( $tax_post ->ID); ?></span> </li> <?php endforeach ; ?> </ul> <?php endif ; endforeach ; endif ; ?> |
さらにこの記事に書くが
何件以上あった場合には 全て表示を表示したい
まずタームなので
上記コードの
1 2 3 4 | $taxonomys = get_terms( $taxonomy_name , $args ); を使い $count = $taxonomy_term -> count ; |
とし
それで分岐 wp_reset_queryの前
リンク先は
1 | $url = get_term_link( $taxonomy_term ->slug, $taxonomy_name ); |
を使いそのままえこー
演算子はここを参照
http://www.tohoho-web.com/js/operator.htm
1 2 3 4 5 6 7 8 9 10 | <?php if ( $count > 9): ?> <div style= "text-align:right; padding-top:0px; padding-bottom:15px; padding-right:30px;" > <a href= "<?php echo $url; ?>" style= "color:#FF3300; font-size:13px;" >すべて見る <img draggable= "false" role= "img" class = "emoji" alt= " src= "https://s.w.org/images/core/emoji/13.1.0/svg/25b6.svg" ><img draggable= "false" role= "img" class = "emoji" alt= " src= "https://s.w.org/images/core/emoji/13.1.0/svg/25b6.svg" ></a> </div> <?php endif ; ?> |
参考
1 2 3 4 5 6 7 8 9 | タームIDで記事数取得: get_term( $chosen_id , 'タクソノミースラッグ' )-> count タームスラッグで記事数取得: get_term_by( 'slug' , 'タームスラッグ' , 'タクソノミースラッグ' )-> count 参照:codex.wordpress.org get_term get_term_by |
カテゴリは
1 | get_category( $id )->category_count; |
これらしい
未検証