サポート » 使い方全般 » 一覧ページでカスタムタクソノミーによって条件分岐

  • お願い致します。

    投稿の記事ひとつひとつが、複数のカスタムタクソノミーに属しています。
    single.phpでは、CODE1を書くことで、その記事が属するタクソノミーを表示することができ、また、属するカスタムタクソノミーによって条件分岐させることもできました。

    トップページやタクソノミーアーカイブページで一覧表示をするときにも、その記事が属するタクソノミーを表示させたり、タクソノミーによって条件分岐させたいのですが、1件目の記事の分しか表示されず、2件目以降は表示されなくなってしまいます(CODE2)。

    CODE3のように、一部を削除すると、全記事のカスタムタクソノミー一覧が表示されますので、is_object_in_term( $post->ID, 'rain', 'rain-ok')が原因のような気がしています。
    その解決方法が分かりません。

    やりたいことは、記事一覧で、特定のカスタムタクソノミーに属する(例:お天気カテゴリで雨に属する)かどうかを判別して条件分岐する、ということを記事ごとに行いたい、ということです。

    is_object_in_term( $post->ID, 'rain', 'rain-ok')は、rainというカスタムタクソノミーが、rain-okというタームであるかどうかを判別するために使っています。
    (in_categoryのような条件分岐タグの代替です。in_taxがなぜか動かなかったので。)

    CODE1:single.php

    <?php while ( have_posts() ) : the_post(); ?>
    	<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    		<h1 class="entry-title"><?php the_title(); ?></h1>
    
    		<div class="weather">
    			<?php
    			$rain_img_path = get_bloginfo('wpurl') . '/wp-content/themes/mytheme/images/icons/rain_ok.png';
    			$fine_img_path = get_bloginfo('wpurl') . '/wp-content/themes/mythem/images/icons/fine.png';
    			if ( is_object_in_term( $post->ID, 'rain', 'rain-ok' ) ) {
    				echo '<a href="http://example.jp/rain/rain-ok"><img src="'.$rain_img_path.'" alt="雨でもOK!" /></a>';
    			} else {
    				echo '<a href="http://example.jp/rain/rain-ok"><img src="'.$fine_img_path.'" alt="晴れの日のみ" /></a>';
    			}
    			?>
    		</div>
    
    		<dl class="taxonomy-list">
    			<dt class="taxons_dt">種類</dt><dd class="taxons_dd"><?php the_category( ' | ', 'multiple' );  ?></dd>
    			<dt class="taxons_dt">エリア</dt><dd class="taxons_dd"><?php echo get_the_term_list( $post->ID, 'prefecture', '', ' | ', '' ) ?></dd>
    			<dt class="taxons_dt">遊び時間</dt><dd class="taxons_dd"><?php echo get_the_term_list( $post->ID, 'hours_needed', '', ' | ', '' ) ?></dd>
    		</dl>			
    
    	</article>
    <?php endwhile; // end of the loop. ?>

    CODE2:index.phpなどのアーカイブページ

    <?php if ( have_posts() ) : ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<h1 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    
    			<div class="weather">
    				<?php
    				$rain_img_path = get_bloginfo('wpurl') . '/wp-content/themes/mytheme/images/icons/rain_ok.png';
    				$fine_img_path = get_bloginfo('wpurl') . '/wp-content/themes/mytheme/images/icons/fine.png';
    				if ( is_object_in_term( $post->ID, 'rain', 'rain-ok' ) ) {
    				echo '<a href="http://example.jp/rain/rain-ok"><img src="'.$rain_img_path.'" alt="雨でもOK!" /></a>';
    				} else {
    				echo '<a href="http://example.jp/rain/rain-ok"><img src="'.$fine_img_path.'" alt="晴れの日のみ" /></a>';
    				}
    				?>
    			</div>
    
    			<dl class="taxonomy-list">
    				<dt class="taxons_dt">種類</dt><dd class="taxons_dd"><?php the_category( ' | ', 'multiple' );  ?></dd>
    				<dt class="taxons_dt">エリア</dt><dd class="taxons_dd"><?php echo get_the_term_list( $post->ID, 'prefecture', '', ' | ', '' ) ?></dd>
    				<dt class="taxons_dt">遊び時間</dt><dd class="taxons_dd"><?php echo get_the_term_list( $post->ID, 'hours_needed', '', ' | ', '' ) ?></dd>
    			</dl>
    		</article>
    	<?php endwhile; ?>
    <?php endif; ?>

    CODE3:こうすると、全記事分のタクソノミーリストが表示される

    <?php if ( have_posts() ) : ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<h1 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    
    			<dl class="taxonomy-list">
    				<dt class="taxons_dt">種類</dt><dd class="taxons_dd"><?php the_category( ' | ', 'multiple' );  ?></dd>
    				<dt class="taxons_dt">エリア</dt><dd class="taxons_dd"><?php echo get_the_term_list( $post->ID, 'prefecture', '', ' | ', '' ) ?></dd>
    				<dt class="taxons_dt">遊び時間</dt><dd class="taxons_dd"><?php echo get_the_term_list( $post->ID, 'hours_needed', '', ' | ', '' ) ?></dd>
    			</dl>
    		</article>
    	<?php endwhile; ?>
    <?php endif; ?>

    どうぞよろしくお願いします。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • #表示されないのは get_the_term_list() の出力ですか?

    WP3.1.3で index.php のコードを試しましたが何も問題なく表示されました。
    1件目表示以降に何かエラーが出てるかも、を調べるのに
    wp-config.php に

    define('WP_DEBUG', true);

    と書いてみると良いかもです。

    ちなみに in_tax() は存在しない気がします。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「一覧ページでカスタムタクソノミーによって条件分岐」には新たに返信することはできません。