サポート » 使い方全般 » single.phpでその記事に属する子ターム記事のタイトル一覧

  • single.phpのサイドバーにその記事に属する子ターム名と子ターム記事タイトル一覧を表示させたいのですが
    タクソノミー名:years
    親ターム名:2000年代 slug名:2000s
    子ターム名:2000年   slug名:2000
          2001年   slug名:2001

    親ターム名:2010年代 slug名:2010s
    子ターム名:2010年   slug名:2010
          2011年   slug名:2011

    まずその記事が属する子タームのslug名を表示させようと

    <?php
    $terms = get_the_terms( $post -> ID, 'years');
    $count = count($terms);
    if($count>0){
    foreach ( $terms as $term ) { 
    $termparent = $term -> parent;
    $termlink = get_term_link( $term, $taxonomy );
    $childname = $term -> slug;
    $childlink = $termlink;
    }
    echo $childname;
    }
    ?>

    としましたが、何故か
    2001年は2001と上手く表示されたのですが
    2000年は2000sと言う親タームのslug名が表示されてしまいました。
    これは2010年代も2010年は2010sと同じでした。

    もう一つ
    その記事と同じ子タームの記事タイトル一覧を表示させたいのですがどうも分かりません。
    query_posts( $args );
    を使うと指定したタームがすべて表示されてしまいます。
    singleページでその記事に属した子タームのみの記事タイトルを表示するにはどうしたらよいのでしょうか?

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック投稿者 apple560

    (@apple560)

    いろいろなサイトを参考に、なんとか子タームのslug名と同じ子タームの記事タイトルが表示するようになりましたが、記事タイトルが10件しか表示しません。また古い順に表示させたいのですが ’tax_query’ => array( )の部分に以下のものを入れても何も反映しませんでした。
    ‘posts_per_page’ => -1, ‘orderby’ => ‘date’, ‘order’ => ‘ASC’
    ↑↑これはどこへ入れたら良いのでしょうか?

    <ul>
    <?php $cats = get_the_terms( 0, 'years' );
    $current_cat = '';
    foreach ( $cats as $cat ) {
    	if ( ! $current_cat || cat_is_ancestor_of( $current_cat, $cat ) ) {
    		$current_cat = $cat; 
    	}
    } $parent = get_term( $current_cat->parent,'years' ); ?>
    <?php
    $categories = get_categories('taxonomy=years&hide_empty=0&child_of=' .$current_cat->parent );
    foreach($categories as $category) :
    ?>
    <?php if ($category->slug == $current_cat->slug): ?>
    <li><?php echo $category->slug; ?></li>
    <?php
    $args = array(
    	'tax_query' => array( 
    		array(
    			'taxonomy'=>'years',
    			'terms'=>array( $category->cat_ID ),
    			'field'=>'id',
    			'posts_per_page' => -1,
    			'orderby' => 'date',
    			'order' => 'ASC',
    			'operator'=>'IN'
    			),
    		)
    );
    $query = new WP_Query( $args );
    ?>
    <!--ここから記事タイトル-->
    <?php if($query->have_posts()): ?>
    <?php while($query->have_posts()): $query->the_post(); ?>
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    <?php endif; ?>
    <!--記事タイトルEND-->
    <?php wp_reset_query(); ?>
    <?php endif; ?><?php endforeach; ?></ul>
1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「single.phpでその記事に属する子ターム記事のタイトル一覧」には新たに返信することはできません。