タクソノミー ターム別 記事一覧  記事件数で全て見る

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;

これらしい
未検証

foreach 繰り返す場合と繰り返さない場合があるとき

1
<?php foreach((array)$others as $other){?>

これだけでいけた

1
2
3
foreachというのは配列の数だけ繰り返すということですので
$list[$key]; が1つの時には配列で返ってこないからエラーが出るということだと思います。
配列かどうかの判断はis_array()で判定させます。

wp-custom-fields-search で各項目にクラスをつけるコード

昔使った 繰り返せば増えるコードを使用

wp-custom-fields-search-form.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php if($title && !(@$params['noTitle'])){
    echo $params['before_title'].$title.$params['after_title'];
}?>
    <form method='get' class='<?php echo $formCssClass?>' action='<?php echo $formAction?>'>
<?php echo $hidden ?>
        <div class='searchform-params'>
         
         
<?php $i = 1; // 追加?>
<?php        foreach($inputs as $input){?>
<div class='<?php echo $input->getCSSClass()?> car_search<?php echo $i++; // 追加?>'><?php echo $input->getInput()?></div>
<?php        }?>
</div>
<div class='searchform-controls'>
 
<input type='submit' name='search' value='<?php _e('Search','wp-custom-fields-search')?>'/>
</div>
<?php if(array_key_exists('showlink',$config) && $config['showlink']) { ?>
<div class='searchform-spoiler'><?php echo $spoiler_link; ?></div>
<?php } ?>
</form>

ブログでサイドバーに色々

カスタムポストの新着情報

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
  <h3 class="widgettitle">施工例 NEW !!</h3>       
 
          <?php
$args = array(
'posts_per_page' => 3,
'post_type' => 'jirei_new', //カスタム投稿名
    );
?>
<?php
query_posts( $args );
if ( have_posts () ) :
    while ( have_posts() ) :
        the_post();
?>
<div class="popular_one">
<div class="popular_sam">
 <?php
  
 $i = 0;
$kiji = 1;//この回数でフォーチ終わる。
 
$fields = $cfs->get('jirei_imgs');
foreach ($fields as $field) :
if($i >= $kiji)://siが3よりおおきくなったら
break;//ループおわり
else://それまではこれで出力
?>
 
<?php
$title= get_the_title();
 $attachment_id = $field['jirei_img'];
 $sample_photo = wp_get_attachment_image($attachment_id,'jirei_small',0,array('alt'=>$title,'title'=>$title));?>
 <a href="<?php the_permalink(); ?>">
<?php
echo $sample_photo;
?>
</a>       
<?php
$i++;//多分繰り返すごとに$iに数値が1つづ増える
endif;
endforeach;
?>
</div>
 
<h4><div class="popular_title"><a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a></div></h4>
<div class="popular_date" style="padding-top:5px;">
<p><?php get_trim_str(array('str'=>get_post_meta($post->ID,"jirei_disp",true), 'len'=>100)); ?></p>
 
</div>
 
</div>
 
<?php
    endwhile;
endif;
?>
 
<!-----------------リセットクエリ--------->
<?php wp_reset_query(); ?>

新着ブログ

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
<h3 class="widgettitle">新着ブログ</h3>      
          <?php
$args = array(
'posts_per_page' => 3,
'cat' => 4,
    );
?>
<?php
query_posts( $args );
if ( have_posts () ) :
    while ( have_posts() ) :
        the_post();
?>
<div class="popular_one">
<div class="popular_sam"><a href="<?php the_permalink(); ?>">
            <?php
            $title= get_the_title();
            the_post_thumbnail('side_new_post_sam',
            array('class' => 'blog_sam_img','alt' =>$title, 'title' => $title)
            ); ?>
            </a></div>
<div class="popular_date"><?php the_time('Y/m/d'); ?></div>
<div class="popular_title"><p><a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a></p></div>
</div>
 
<?php
    endwhile;
endif;
?>
 
<!-----------------リセットクエリ--------->
<?php wp_reset_query(); ?>

新着 サムネイルなし

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
<h3 class="widgettitle">Information</h3>      
          <?php
$args = array(
'posts_per_page' => 3,
'cat' => 5,
    );
?>
<?php
query_posts( $args );
if ( have_posts () ) :
    while ( have_posts() ) :
        the_post();
?>
<div class="popular_one">
<div class="popular_sam"><a href="<?php the_permalink(); ?>">
            <?php
            $title= get_the_title();
            the_post_thumbnail('side_new_post_sam',
            array('class' => 'blog_sam_img','alt' =>$title, 'title' => $title)
            ); ?>
            </a></div>
<div class="popular_date"><?php the_time('Y/m/d'); ?></div>
<div class="popular_title"><p><a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a></p></div>
</div>
 
<?php
    endwhile;
endif;
?>
 
<!-----------------リセットクエリ--------->
<?php wp_reset_query(); ?>

css

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
/*-----------------------------------------------------ウィジェット用-----------------------------------------------------*/
 
.widgettitle{
    font-size: 12px;
    margin-top: 20px;
    margin-bottom: 30px;
    line-height: 44px;
    height: 40px;
    background-color: #595959;
    -webkit-border-top-left-radius: 5px;
    -webkit-border-top-right-radius: 5px;
    -webkit-border-bottom-right-radius: 5px;
    -webkit-border-bottom-left-radius: 5px;
    -moz-border-radius-topleft: 5px;
    -moz-border-radius-topright: 5px;
    -moz-border-radius-bottomright: 5px;
    -moz-border-radius-bottomleft: 5px;
    color: #FFF;
    padding-left: 15px;
    border-left-width: 4px;
    border-left-style: solid;
    border-left-color: #e50012;
}
     
.widget{
    list-style-type: none;
}
 
/*-----------------------------------------------------wp popular postプラグイン用-----------------------------------------------------*/
 
.popular-posts{}
.popular_one{
    margin-bottom: 8px;
    padding-bottom: 8px;
    border-bottom-width: 1px;
    border-bottom-style: dotted;
    border-bottom-color: #CCC;
    clear: both;
    overflow: hidden;
}
.popular_sam{
    float: left;
}
.popular_sam img{
    float: left;
    background-color: #FFF;
    padding: 1px;
    border: 1px solid #CCC;
    margin-right: 12px;
}
.popular_date{
    font-size: 10px;
}
.popular_title{
    font-size: 12px;
}
 
.wpp-views{
    color: #FF7737;
}

楽天の様な複数パン屑リスト シングル用複数行パン屑

複数パン屑参考
http://ja.forums.wordpress.org/topic/5176

カテゴリーを情報取得後フォーチループ

1
2
3
4
5
6
7
8
9
10
11
<?php
        $category = get_the_category();
    foreach( $category as $cat ){
//        echo '<a href="ホームページのURL">Home</a> > ';
    echo '<a href="ホームページのURL/category/' . $cat->category_nicename . '/">';
    echo $cat->cat_name;
    echo  '</a> >';
        echo the_title();
        echo '<br />';
        }
?>

フォーチの並び替え参考
http://masago.kir.jp/php20030119.php

1
2
3
ksort($category,SORT_STRING);//フォーチの並び替え指示 これ使用
asort($category,SORT_NUMERIC);//フォーチの並び替え指示
asort($category,SORT_STRING);//フォーチの並び替え指示 これ機能せず

フォーチの回数指定参考
http://www.geekzshu.com/php/509

1
2
3
4
5
6
7
8
9
10
11
12
13
$i = 0;
$kiji = 5;
foreach ($rss->items as $item ) {
if($i >= $kiji){
break;}
else{
$title = $item['title'];
$link = $item['link'];
echo "<li><a href="$link">$title</a></li>n";
$i++;
}
}

シングル用複数パン屑完成 0561仕様

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
<!--------------------------------------------------------------------------------------------------------------------------------------------------パンくずリスト--->
<div id="top_navi">
 
<?php $urls = home_url();
 
        $category =  get_the_terms( 0, 'genre' );
    ksort($category,SORT_STRING);//フォーチの並び替え指示
     
$i = 0;
$kiji = 3;//この回数でフォーチ終わる。
     
    foreach( $category as $cat ){
         
if($i >= $kiji){//siが3よりおおきくなったら
break;}//ループおわり
else{//それまではこれで出力
         
         
         
        $urls2=get_term_link($cat->slug,'genre');//$urls2にゲットタームリんくでとったもの入れた。
         
    echo '<a href="' .$urls. '">0561navi</a> > ';
     
     //$termにはいってる情報で分岐
if ( $cat->parent ) {
// 親IDが入ってれば (子だったら)
//親のIDを取得してターム情報を引き出す。
$ido=$cat->parent;
$term2=get_term($ido,'genre');
?>
<a href="<?php //親のターム情報からターム名とリンクを引き出す。
echo get_term_link($term2->slug,'genre'); ?>
">
<?php
echo $term2->name; ?>
</a> >
<?php
}          
    echo '<a href="' .$urls2. '">';
    echo $cat->name;
    echo  '</a> > ';
        //echo the_title();
        echo '<br />';
        $i++;//多分繰り返すごとに$iに数値が1つづ増える
        }//回数指定終了
        }
?>
 
<div style="clear:both; padding-bottom:20px;"></div>
 
 
 
</div>