mogmogxxx
フォーラムへの返信
-
フォーラム: 使い方全般
返信が含まれるトピック: カスタム投稿のarchiveで親子タームを維持した記事一覧表示gblsmさん
またまたアドバイスいただきありがとうございます!
いただいたコードにて実装してみたところ思った通りの挙動になっております!!
勉強不足の中で大変恐縮ですが、本当にありがとうございました。
助かりました!こちらにてこの質問は解決済みとさせていただきます。
フォーラム: 使い方全般
返信が含まれるトピック: カスタム投稿のarchiveで親子タームを維持した記事一覧表示連投すいません。。。
<?php $categories = get_terms('category01_classify','orderby=order&order=ASC&parent=0'); foreach ( $categories as $cat ) { echo '<h1>' . esc_html($cat->name) . '</h1>'; $children = get_terms('category01_classify','hierarchical=0&orderby=order&order=ASC&child_of='.$cat->term_id); foreach ( $children as $child ) { echo '<h2>' . esc_html($child->name) . '</h2>'; $catslug = $child->slug; $args = 'post_type=category_01&category01_classify=' . $catslug . '&showposts=-1'; query_posts( $args ); if ( have_posts() ) : $output .= '<ul>'; while ( have_posts() ) : the_post(); $output .= '<li><a href=" '. get_permalink() .' ">'; $output .= get_the_title() .'</a></li>'; endwhile; $output .= '</ul>'; echo $output; wp_reset_query(); endif; } } ?>
参考URL
https://ja.forums.wordpress.org/topic/7786?replies=7↑を参考に、汚いですが書き換えてみました。
【表示】
category01_01category01_02
- category01_02_01
– 記事02
– 記事03
- category01_02_02のように表示され、親タームに属している記事だけ表示されません。
(phpでechoしていないので当たり前なのですが悩んでいます。。)なにかアドバイスいただけないでしょうか。。
フォーラム: 使い方全般
返信が含まれるトピック: カスタム投稿のarchiveで親子タームを維持した記事一覧表示gblsmaさん ご返信ありがとうございます!
無知でもうしわけありません。
いただいたコードを参考にしたのですが、termsでtermを設定しなければならないので複数設定しなければいけない感じなのでしょうか?私が書いたコード↓
`
<?php
$args = array(
‘post_type’ => ‘category_01’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘category01_classify’,
‘field’ => ‘slug’,
‘terms’ => ‘category01_02_01’,
‘include_children’ => false
)
)
);
$my_posts = get_posts( $args );
if ( $my_posts ) {
foreach ( $my_posts as $post ) :
setup_postdata( $post );
?>
<li>
<dl>
<dt><?php the_title(); ?></dt>
<dd><a href=”<?php the_permalink(); ?>”>リンク</a></dd>
</dl>
</li>
<?php
endforeach;
} else {
echo ‘このカテゴリに投稿はありません’;
}
wp_reset_postdata();
?>------ 【表示目標】 category01_01 - 記事01 category01_02 - category01_02_01 -- 記事02 -- 記事03 - category01_02_02 はこのように、記事が子タームに属している場合には、親ターム直下には表示されずに入れ子関係が維持されているような形です。 【記事が子タームに属していたら○○】【それ以外は○○(親ターム直下に表示する】のような書き方ができるといいなと思っているのですが、難しいのでしょうか。。。。 ---- ちなみに、一番はじめに参考URLを参考にしていたときの書き方は↓になります。 これだと、一番はじめに投稿させていただいたような2重に表示されてしまいました。
<?php
$catargs = array(
‘taxonomy’ => ‘category01_classify’
);
$catlists = get_categories( $catargs );
foreach($catlists as $cat) :
?>
<li class=”head”><?php echo $cat->name; ?></li>
<?php
$args = array(
‘post_type’ => ‘category_01’,
‘category01_classify’ => $cat->slug
);
$my_posts = get_posts( $args );
if ( $my_posts ) { // 該当する投稿があったら
foreach ( $my_posts as $post ) :
setup_postdata( $post );
?>
<li class=”lv1″>
<dl>
<dt><?php the_title(); ?></dt>
<dd><a href=”<?php the_permalink(); ?>”>リンク</a></dd>
</dl>
</li>
<?php
endforeach;
} else {
echo ‘このカテゴリに投稿はありません’;
}
wp_reset_postdata();
?>
<?php endforeach; ?>
`—-
お知恵ばかり拝借するようで恐縮ですが、よろしくお願いします。