サポート » 使い方全般 » カスタムフィールドを参照してカテゴリーごとの一覧表示が出来ないのです

  • 解決済 kleza

    (@kleza)


    お世話になっております。

    category.phpにおいて
    カスタムフィールドの値(日付)で
    出力しているのですが
    カテゴリーごとの表示にした際に
    そうはならず、ただ日付の条件に
    合致したものがすべて表示されてしまいます。

    category.phpでは
    こういった処理(カスタムフィールドの
    値に照らした上で表示する)は
    出来ないのでしょうか?

    私の以下の記述が間違っているのでしょうか。

    お手数ですがアドバイス等
    いただけると大変助かります。

    何卒よろしくお願いいたします。

    category.php

    
    	<?php
    		$nowdate = time();
    		$args = array(
    			'post_type' => 'post',     /* 投稿タイプ */
    			'meta_value' => $nowdate, /* 現在の日付を基準 */
    			'orderby' => 'meta_value',   /* ソートはmeta_valueを基準に */
    			'meta_key' => 'wpcf-start', /* 比較するmeta_key */
    			'meta_compare' =>'>=',        /* meta_keyがvalueより大いものだけ */
    			'order' => 'ASC'   /* 昇順に並び替え  */
    		);
    		$query = new WP_Query( $args );
    	?>
    	<?php if($query -> have_posts()): 
    	while($query -> have_posts()): $query -> the_post(); ?>
    		<div class="entry">
    			<div class="body">
    				<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    				<ul class="opt_info">
    					<li class="date">
    						<?php echo(types_render_field("start", array("format" => "Y年m月d日(D) H:i"))); ?> 〜 <?php echo(types_render_field("end", array("format" => "H:i"))); ?>
    					</li>
    					<li class="area">
    						<?php echo(types_render_field("area", array())); ?>
    						(<?php echo(types_render_field("prefecture", array())); ?>)
    					</li>
    					<li class="category"><?php the_category(', '); ?></li>
    					<li class="tag"><?php the_tags(''); ?></li>
    				</ul>
    				<?php echo my_excerpt(100); ?>
    			<!-- body --></div>
    		<!-- entry --></div>
    	<?php endwhile; ?>
    	<?php endif; ?>
    	<?php wp_reset_postdata(); /* WP_Queryのループをリセット */ ?>
    
4件の返信を表示中 - 1 - 4件目 (全4件中)
  • カテゴリーごとの表示にした際に

    というのは、たとえば news というカテゴリーがあるとして ‘//(domain)/category/news/ ‘ で news カテゴリーアーカイブを表示する、ということだと思いますが、出力する際に category.php の中で日付条件のみのクエリーに書き換えているので、当然そうなってしまうかと思います。

    category.php の中で条件を設定するのでなく、pre_get_posts フックを利用してカテゴリーアーカイブに条件を追加してはいかがでしょうか。

    たとえばテーマの functions.php などに下記のようなコードを入れて、category.php からは $args = array(・・・);$query = new WP_Query( $args ); を取り除けば、目的の結果が得られるような気がします。

    function my_custom_query_for_category( $query ) {
    	if ( is_category() ) {
    		$nowdate = time();
    		$query->set( 'meta_value', $nowdate );
    		$query->set( 'orderby', 'meta_value' );
    		$query->set( 'meta_key', 'wpcf-start' );
    		$query->set( 'meta_compare', '>=' );
    		$query->set( 'order', 'ASC' );
    	}
    }
    add_action( 'pre_get_posts', 'my_custom_query_for_category' );
    
    • この返信は6年、 4ヶ月前にkazuykが編集しました。理由: コードブロックの閉じ忘れ
    トピック投稿者 kleza

    (@kleza)

    丁寧にありがとうございます。

    アドバイスにしたがってfunctions.php、category.phpを
    書き換えたのですが、以下のエラーが発生します。

    何が間違っているのでしょうか?
    丁寧に説明していただいているのに、理解できていなくてすいません。

    エラー内容

    Fatal error: Uncaught Error: Call to a member function have_posts() on null in /home/nakadachiuri/public_html/_wp/wp-content/themes/zen/category.php:9 Stack trace: #0 /home/nakadachiuri/public_html/_wp/wp-includes/template-loader.php(74): include() #1 /home/nakadachiuri/public_html/_wp/wp-blog-header.php(19): require_once(‘/home/nakadachi…’) #2 /home/nakadachiuri/public_html/_wp/index.php(17): require(‘/home/nakadachi…’) #3 {main} thrown in /home/nakadachiuri/public_html/_wp/wp-content/themes/zen/category.php on line 9

    functions.php

    function my_custom_query_for_category( $query ) {
    	if ( is_category() ) {
    		$nowdate = time();
    		$query->set( 'meta_value', $nowdate );
    		$query->set( 'orderby', 'meta_value' );
    		$query->set( 'meta_key', 'wpcf-start' );
    		$query->set( 'meta_compare', '>=' );
    		$query->set( 'order', 'ASC' );
    	}
    }
    add_action( 'pre_get_posts', 'my_custom_query_for_category' );
    

    category.php

    <?php if($query -> have_posts()): 
    while($query -> have_posts()): $query -> the_post(); ?>
    	ループの内容
    <?php endwhile; ?>
    <?php endif; ?>

    category.php のループの $query-> が不要ですね。

    トピック投稿者 kleza

    (@kleza)

    さっそくのコメントありがとうございます!
    望む結果が得られました。

    お恥ずかしいほどの不勉強さに対して
    丁寧、親切にご教示くださりありがとうございました!

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「カスタムフィールドを参照してカテゴリーごとの一覧表示が出来ないのです」には新たに返信することはできません。