home.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 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 |
<div class="box"> <?php $args = array( 'parent' => 0, 'hierarchical' => 0, 'order' => 'ASC' ); $taxonomy_name = 'タクソノミー名'; $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 ); ?> <div class="contentbox01"> <h2 id="<?php echo esc_html($taxonomy->slug); ?>" class="h2_style01"><?php echo esc_html($taxonomy->name); //タームタイトル?></h2> <div class="contentbox01_in"> <table class="table_style01"> <?php /** * お知らせ等の一覧を取得 * */ $args = array( 'post_type' => 'カスタム投稿名', 'posts_per_page' => 10, //新着順に10件表示 'orderby' => 'post_date', 'order' => 'DESC', 'post_status' => 'publish', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'タクソノミー名', 'field' => 'slug', 'terms' => $taxonomy->slug, 'include_children' => true, 'operator' => 'IN' ), ) ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ): ?> <?php $term_id = $taxonomy->term_id; $children = get_term_children( $term_id, $taxonomy_name ); if ( $children ): //子タームを持つ場合にはタームの一覧を表示 ?> <?php $term_id = $taxonomy->term_id; $taxonomy_name = 'タクソノミー名'; $termchildren = get_term_children( $term_id, $taxonomy_name ); foreach ( $termchildren as $child ) { $term = get_term_by( 'id', $child, $taxonomy_name ); echo '<tr><td><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a><td></th>'; } ?> <?php else: ?> //子タームを持たない場合には投稿の一覧を表示 <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <tr> <th> <?php the_time('Y.m.d'); ?> </th> <td> <?php $cf_important = get_field( 'important' ); //ACFで作成したチェックボックス if ( $cf_important == '表示する' ) { echo '<span class="important01">重要</span>'; } else { echo ' '; } ?> <?php if(get_field('important')): ?> <span class="important01">重要</span> <?php endif ;?> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </td> </tr> <?php endwhile; ?> <?php endif; ?> <?php endif; wp_reset_postdata(); ?> </table> </div> <div class="btnbox01"> <a href="<?php echo esc_url( home_url( '/' ) ); ?><?php echo esc_html($taxonomy->slug); ?>/">一覧へ</a> </div> </div> <?php endforeach; endif; wp_reset_postdata(); ?> </div> <!-- box --> |
コメント