サポート » 使い方全般 » カスタムフィールドの値を元に一覧にした際のページ送り

  • 解決済 ukired

    (@ukired)


    AカテゴリのBというカスタムフィールドに「あ」という値を持っている記事を一覧にしたいと思いました。
    以下URLを参考に、カスタムフィールドの値を元に、category.phpで一覧を並べる事はできたのですが、
    ページネーションを設定した際にうまくいきません、どうにか解決できると嬉しいです。

    ▼一覧の参考
    https://wordpress.org/support/topic/how-to-get-a-meta-values-from-all-posts-in-a-special-category/

    ■ページネーションの動作状態
    ページネーションを表示することはできているのですが、
    ページ送りをして/page/2/を表示しても、表示される内容が変わらない状態です。

    ■category.phpの表記

    
    <?php
    	foreach((get_the_category()) as $cat) {
    	$cat_id = $cat->cat_ID ;//カテゴリーIDの取得
    	break ;//ループ終了
    	}
    	//カテゴリー&カスタムフィールド名=値で10件並べる(テストのため現在1件にしています)
    	$paged = get_query_var('paged') ? get_query_var('paged') : 1;
    	query_posts( 'cat=' . $cat_id. '&posts_per_page=1&paged=$paged&meta_key=' . $_GET['key'] . '&meta_value=' . $_GET['value']);
    ?>
    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
    
    〜内容〜
    
    <?php  endwhile; endif;
    	//posts_nav_link でページ送り
    	posts_nav_link(' | ', 'Prev', 'Next');
    	wp_reset_query();
    ?>
    

    URLは
    【http://〜/category/A/?key=type_B&value=あ】
    のような形で、一応絞り込んでの一覧表示はできています。

    ページ送りをすると【http://〜/category/A/page/2/?key=type_B&value=あ】となり、
    404などにはなっていません。
    試しに、【http://〜/category/A/】でカテゴリーごとのページ送りも試してみましたが同様でした。

    ループの書き方がまずいのでしょうか、、
    お知恵をおかしいただけるとうれしいです。

    • このトピックはukiredが6年、 1ヶ月前に変更しました。
1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック投稿者 ukired

    (@ukired)

    海外の方でアドバイス頂き、自己解決しました。
    こちらにも書いておきます。
    query_postから、pre_get_postsに変更しました。

    function.php

    
    //pre_get_posts
    function pre_get_posts_custom($query) {
    	if( is_admin() || ! $query->is_main_query() ){
    	return;
    }
    /*=====category===== */
    if ( $query->is_category() ) {
    	$slug = $query->query_vars['category_name']; 
    	$cat = get_category_by_slug($slug);
    	$cat_id = $slug->cat_id;
    	$query->set( 'posts_per_page', '2');
    	$query->set( 'cat', $cat_id );
    	$query->set( 'orderby', 'meta_value');
    	$query->set( 'meta_key', $_GET['key']);
    	$query->set( 'meta_value', $_GET['value']);
    	return;
    	}
    }
    add_action( 'pre_get_posts', 'pre_get_posts_custom' );
    

    category.php

    
    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
    
    <!--post list-->
    
    <?php  endwhile; endif; ?>
    <nav id="pager">
    	<ul id="pager_ul">
    		<?php $maxpage = $wp_query->max_num_pages;
    			$current = $paged;
    			if(!$current){$current = 1;}
    		?>
    		<?php if(!($maxpage==1)): ?>
    			<li id="prev"><?php previous_posts_link('前へ'); ?></li>
    		<?php for($i=1; $i <= $maxpage; $i++): ?>
    		<?php if($i == $current): ?>
    			<li class="current"><?php echo $i; ?></li>
    		<?php else: ?>
    			<li><a href="<?php echo get_pagenum_link($i); ?>"><?php echo $i; ?></a></li>
    		<?php endif; ?>
    		<?php endfor; ?>
    			<li id="next"><?php next_posts_link('次へ'); ?></li>
    		<?php endif; ?>
    	</ul>
    </nav><!-- / #pager -->
    
    <?php  wp_reset_query(); ?>
    

    posts_nav_link でのページ送りから、
    変更の都合で最終的に数字のページネーションにすることになりましたので上記の表記に変えました。

    • この返信は6年、 1ヶ月前にukiredが編集しました。
1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「カスタムフィールドの値を元に一覧にした際のページ送り」には新たに返信することはできません。