前提・実現したいこと
WPのfuntions.phpで追加したカスタム投稿タイプの投稿を、カテゴリ一覧に管理画面で設定したとおりの件数で表示したい。
発生している問題・エラーメッセージ
funtions.phpでカスタム投稿タイプを追加し、7件の投稿をしましたが、一覧ページcategory-question.phpに1件しか表示されません。
管理画面の表示件数設定は6件です。
どうも投稿自体を1件しか取れていないようです。
該当のソースコード
funtions.php(カスタム投稿タイプ追加部分)
function create_post_type() { register_post_type( 'question', array( 'labels' => array( 'name' => __( 'FAQ' ), ), 'supports' => array('title', 'editor', 'thumbnail'), 'public' => true, 'menu_position' =>5, )); } add_action( 'init', 'create_post_type' );
funtions.php(カスタム投稿タイプのクエリ取得部分)
function pre_get_posts_custom( $query ) { if ( is_admin() || !$query->is_main_query() ) { return; } if ( is_category('question') ) { $query -> set('post_type', 'question'); } } add_action('pre_get_posts','pre_get_posts_custom');
category-question.php
<?php if ( have_posts() ) : $x=1; ?> <?php while ( have_posts() ) : the_post(); echo $x; $x++; ?> <article> <h1><?php the_title(); ?></h1> <p><?php the_excerpt(); ?></p> </article> <?php endwhile; else : ?> <p>記事がないときはこっち</p> <?php endif; ?>
現在の状況
管理画面の表示件数設定は6件、投稿数は7件ですが、最新記事1件と$xの結果1が表示されています。
試したこと
もともとWP_Queryで書いていたのですが、どうしてもページネーションがうまくいかなくてpre_get_postsに切り替えたところです。
https://e-joint.jp/450/#toc-item-2
ここを参考に、
if ( is_category('question') ) {
を
if ( $query->is_category('question') ) {
にしてみたりしましたが変わりませんでした。
(サイトによって$query->がついていたり無かったりするので、関係ないかなとは思いました)
何時間も悩みましたが解決しません。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー