【WordPress】カスタム投稿記事の一覧を取得する方法メモ

カスタム投稿タイプの記事一覧を表示したい場所の取得方法メモ。
以下のコードはデフォルトの投稿を取得する内容です。
これをベースに、取得する内容をデフォルトの投稿からカスタム投稿を変更します。
<?php
global $post;
$args = array( 'posts_per_page' => 5 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) {
setup_postdata($post);
?>
<dl>
<dt><a href="<?php the_permalink() ?>"><?php the_post_thumbnail( array(200, 100) ); ?></a></dt>
<dd><?php the_time('Y.m.d') ?> <?php the_category('|') ?>
<h3 class="copy"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
</dd>
</dl>
<?php
}
wp_reset_postdata();
?>
3行目の
【$args = array( ‘posts_per_page’ => 5 );】
を
【$args = array( ‘post_type’ => array(‘ここにタイプ名’), ‘posts_per_page’ => 5);】
に変更します。
<?php
global $post;
$args = array( 'post_type' => array('ここにタイプ名'), 'posts_per_page' => 5);
$myposts = get_posts( $args );
foreach( $myposts as $post ) {
setup_postdata($post);
?>
<dl>
<dt><a href="<?php the_permalink() ?>"><?php the_post_thumbnail( array(200, 100) ); ?></a></dt>
<dd><?php the_time('Y.m.d') ?> <?php the_category('|') ?>
<h3 class="copy"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
</dd>
</dl>
<?php
}
wp_reset_postdata();
?>
これでカスタム投稿の一覧が取得できます。
タグ: WordPress | 2015年12月27日



