Web制作で役立つメモをWEB MEMO LOG

2016.11.06

【WordPress】WP_Queryを使った記事表示の方法

WordPressでの記事の出力方法ですが、毎回毎回検索しているのでメモメモ。

▼WP_Queryの基本的な記述例

<?php
$args = array(
	'cat' => 1,
	'post_type' => 'my_post',
	'posts_per_page' => 10,
	'post_status' => 'publish',
	'paged' => $paged
);
?>

<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) :?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

//記事を表示

<?php endwhile;?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>

【参考】WordPressで押さえておきたい!get_posts,WP_Query,query_posts の違いと用例

About Site

同じことを何度も検索していたりするんで、検索して解決したことを残そうと思いまして。