WordPress「post_per_page」で取得数が指定できない場合の対処

Filed under: php,wordpress — kdcs @ 24年7月5日 金曜日

通常のnew WP_Query内にpost_per_pageで取得するpost数を指定してもダッシュボード>設定>表示設定で指定した表示数になってしまうので以下の記述で対応。

<?php
$query = new WP_Query(
    $args = array(
        'post_type' => 'post',
        'post_per_page' => 3,
    )
);
$query->post_count = 3;
if ( $query->post_count > count( $query->posts ) ) :
	$query->post_count = count( $query->posts );
endif; ?>

<?php if($query -> have_posts()): while($query -> have_posts()): $query -> the_post(); ?>
// ループさせる内容
<?php else : ?>
// 記事が無い場合の記述
<?php endif; wp_reset_postdata(); ?>

サイト内検索

カテゴリー

最近の投稿

« |WordPress「post_per_page」で取得数が指定できない場合の対処| »
↑上に戻る