お世話になります。
親カテゴリ を指定して、子カテゴリ の合計記事数を表示させようとしています。
■北海道・東北(1)
・子:北海道(2)
・子:青森県(1)
・子:岩手県(0)
〜〜以下省略〜〜
北海道・東北が(3)にならず困っています。
北海道・東北カテゴリに該当する記事は3つあるので、合計は(3)と表示されるはずなのですが、
現状は(1)と表示されます。
これはテスト02の記事は、親カテゴリ 北海道・東北にチェックを入れているためです。
親カテゴリにチェックを入れなくても、子カテゴリの合計のみを表示させたいです。
使用しているコードは下記になります。
<td width="18%" align="center" valign="middle" bgcolor="#ccffff"> // リンク付きカテゴリ名を表示 <?php $args = array( 'orderby' => 'name', 'include' => '2' ); $categories = get_categories( $args ); foreach ( $categories as $category ) { echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name .' </a>'; } ?><br/> <?php $args = array( 'orderby' => 'name', 'include' => '2', 'pad_counts' => 1 ); $categories = get_categories( $args ); foreach ( $categories as $category ) { echo . $category->count .; } ?> </td>
include =>'2' が 北海道・東北のカテゴリ ID。
pad_counts =>1 (true) が 子カテゴリの合計記事の取得。
これで表示できると思ったのですが。。
Wordpres Codex 関数リファレンス get terms
このコードで表示させると、記事編集画面で親カテゴリ のチェックも入れないと記事合計の数字が表示できません。
記事編集画面で子カテゴリのみチェックを入れて、合計を表示したいと思っています。
色々試してみましたが、解決できずご質問させていただきました。
ご回答よろしくお願いいたします。
■回答1_miyabi_takatsuk様のコードを反映してみました
■回答2_YukiYamashina様のコードを反映してみました
■コード全体
front-page.php
古いtableレイアウトで組んであるページを
Wordpresへ載せ替えを行っています。
table2段目の関東以下にはphpをまだ反映していません。
<table class="line tcenter" width="734px" border="0" cellpadding="0" cellspacing="1" bgcolor="#666666"> <tbody><tr> <td width="18%" align="center" valign="middle" bgcolor="#ccffff"> <?php $args = array( 'orderby' => 'name', 'include' => '2' ); $categories = get_categories( $args ); foreach ( $categories as $category ) { echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name .' </a>'; } ?><br/> <?php $parent_id = 2; $args = array( 'orderby' => 'name', 'pad_counts' => 1, ); // 全カテゴリーを取得する. $categories = get_categories( $args ); // 親カテゴリーだけを取得する. $parent_category = wp_list_filter( $categories, array( 'term_id' => $parent_id ) )[0]; // 親カテゴリーの投稿数. echo $parent_category->count; // 子カテゴリーだけを取得する. $child_categories = wp_list_filter( $categories, array( 'parent' => $parent_id ) ); foreach( $child_categories as $child_category ) { // 子カテゴリーの投稿数. echo $child_category->count; } ?> </td> <td width="10%" align="center" valign="top" bgcolor="#ccffff"> <?php $args = array( 'orderby' => 'name', 'include' => '13', 'hide_empty' => 0, 'pad_counts' => 1 ); $categories = get_categories( $args ); foreach ( $categories as $category ) { echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name .'</a><br/>('.$category->count .')'; } ?> </td> <td width="10%" align="center" valign="top" bgcolor="#ccffff"> <?php $args = array( 'orderby' => 'name', 'include' => '14', 'hide_empty' => 0, 'pad_counts' => 1 ); $categories = get_categories( $args ); foreach ( $categories as $category ) { echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name .'</a><br/>('.$category->count .')'; } ?> </td> <td width="10%" align="center" valign="top" bgcolor="#ccffff"> <?php $args = array( 'orderby' => 'name', 'include' => '15', 'hide_empty' => 0, 'pad_counts' => 1 ); $categories = get_categories( $args ); foreach ( $categories as $category ) { echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name .'</a><br/>('.$category->count .')'; } ?> </td> <td width="10%" align="center" valign="top" bgcolor="#ccffff"> <?php $args = array( 'orderby' => 'name', 'include' => '16', 'hide_empty' => 0, 'pad_counts' => 1 ); $categories = get_categories( $args ); foreach ( $categories as $category ) { echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name .'</a><br/>('.$category->count .')'; } ?> </td> <td width="10%" align="center" valign="top" bgcolor="#ccffff"> <?php $args = array( 'orderby' => 'name', 'include' => '17', 'hide_empty' => 0, 'pad_counts' => 1 ); $categories = get_categories( $args ); foreach ( $categories as $category ) { echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name .'</a><br/>('.$category->count .')'; } ?> </td> <td width="11%" align="center" valign="top" bgcolor="#ccffff"> <?php $args = array( 'orderby' => 'name', 'include' => '18', 'hide_empty' => 0, 'pad_counts' => 1 ); $categories = get_categories( $args ); foreach ( $categories as $category ) { echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name .'</a><br/>('.$category->count .')'; } ?> </td> <td width="11%" align="center" valign="top" bgcolor="#ccffff"> <?php $args = array( 'orderby' => 'name', 'include' => '19', 'hide_empty' => 0, 'pad_counts' => 1 ); $categories = get_categories( $args ); foreach ( $categories as $category ) { echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name .'</a><br/>('.$category->count .')'; } ?> </td> <td width="10%" align="center" valign="top" bgcolor="#FFFFFF"> </td></tr> <tr> <td align="center" valign="middle" bgcolor="#FFFF99"> </td> <td align="center" valign="top" bgcolor="#FFFF99"><a href="http://www.takamikk.jp/sys/index.asp?ID=216">茨城県</a><br>(116)</td> <td align="center" valign="top" bgcolor="#FFFF99"><a href="http://www.takamikk.jp/sys/index.asp?ID=219">栃木県</a><br>(74)</td> <td align="center" valign="top" bgcolor="#FFFF99"><a href="http://www.takamikk.jp/sys/index.asp?ID=258">群馬県</a><br>(98)</td> <td align="center" valign="top" bgcolor="#FFFF99"><a href="http://www.takamikk.jp/sys/index.asp?ID=164">埼玉県</a><br>(65)</td> <td align="center" valign="top" bgcolor="#FFFF99"><a href="http://www.takamikk.jp/sys/index.asp?ID=270">千葉県</a><br>(83)</td> <td align="center" valign="top" bgcolor="#FFFF99"><a href="http://www.takamikk.jp/sys/index.asp?ID=251">東京都</a><br>(60)</td> <td align="center" valign="top" bgcolor="#FFFF99"><a href="http://www.takamikk.jp/sys/index.asp?ID=246">神奈川県</a><br>(34)</td> <td align="center" valign="top" bgcolor="#FFFFFF"> </td> </tr> ~~~~~~~~~~~~~ 文字数上掲載できなかったので省略。 地域別で繰り返しになっています。 ~~~~~~~~~~~~~ </tbody></table> </article> </section> </div> <!-- / content -->
■回答1_miyabi_takatsuk様のコードで実装できました。
・使用コード
回答2件
あなたの回答
tips
プレビュー