WordPress タクソノミー、ターム、投稿タイプや著者を特定した記事一覧の出力方法(サムネイル出力含む)

この記事は年前に書かれました。不適当な記述を含む場合がありますので、参考程度に留めてください。

どうもこんばんは。今回のTipsは【Wordpress タクソノミー、ターム、投稿タイプや著者を特定した記事一覧の出力方法(サムネイル出力含む)】です。複数のカスタム投稿を駆使すると「カスタム投稿タイプ内の特定カテゴリーの記事一覧の取得」等少し条件を増やした記事一覧を表示したくなる事が有ります。今回の記事はそういった、複数条件下での記事一覧出力方法をまとめたメモになります。では順をおって…

投稿タイプを特定した記事一覧の出力

投稿タイプ【tips】の記事を5件、サムネイル付きで表示する例です。

<?php global $post;
$args = array(
‘post_type’ => ‘tips’,
‘numberposts’ => 5
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);?>
<?php the_title() ?>
<?php the_post_thumbnail(); ?>
<?php endforeach; ?>

複数の投稿タイプで指定した記事一覧の出力

投稿タイプ【tips】及び投稿タイプ【post】の記事を5件、サムネイル付きで表示する例です。

<?php global $post;
$args = array(
‘post_type’ => array(‘post’, ‘tips’),
‘numberposts’ => 5
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);?>
<?php the_title() ?>
<?php the_post_thumbnail(); ?>
<?php endforeach; ?>

タクソノミーを特定した記事一覧の出力

タクソノミー名【tips_category】内のターム名【html】の記事を5件を表示する例です。

<?php global $post;
$args = array(
‘post_type’ => ‘tips’,
‘taxonomy’ => ‘tips_category’,
‘term’ => ‘html’,
‘numberposts’ => 5
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);?>
<?php the_title() ?>
<?php the_post_thumbnail(); ?>
<?php endforeach; ?>

投稿タイプ、タクソノミー、タームを特定した記事一覧の出力

投稿タイプ【tips】、タクソノミー名【tips_category】、ターム名【html】の記事を5件を表示する例です。

<?php global $post;
$args = array(
‘post_type’ => ‘tips’,
‘taxonomy’ => ‘tips_category’,
‘term’ => ‘html’,
‘numberposts’ => 5
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);?>
<?php the_title() ?>
<?php the_post_thumbnail(); ?>
<?php endforeach; ?>

投稿タイプ、タクソノミー、ターム、著者を特定した記事一覧の出力

投稿タイプ等に加えて著者を特定した記事一覧を出力します。例では、著者ID1(admin)に特定しています。array以外は共通ですので、以降割愛します。

$args = array(
‘post_type’ => ‘tips’,
‘taxonomy’ => ‘tips_category’,
‘term’ => ‘html’,
‘author’ => ‘1’,
‘numberposts’ => 5
);

投稿を昇順にした記事一覧の出力

上記に加え投稿を昇順に指定した例です。

$args = array(
‘post_type’ => ‘tips’,
‘taxonomy’ => ‘tips_category’,
‘term’ => ‘html’,
‘author’ => ‘1’,
‘order’ => ‘ASC’,
‘numberposts’ => 5
);

投稿をランダムにした記事一覧の出力

上記に加え投稿をランダムに指定した例です。

$args = array(
‘post_type’ => ‘tips’,
‘taxonomy’ => ‘tips_category’,
‘term’ => ‘html’,
‘author’ => ‘1’,
‘orderby’ => ‘rand’,
‘numberposts’ => 5
);

投稿をコメント数順にした記事一覧の出力

投稿をコメント数順に指定した例です。

$args = array(
‘post_type’ => ‘tips’,
‘taxonomy’ => ‘tips_category’,
‘term’ => ‘html’,
‘author’ => ‘1’,
‘orderby’ => ‘comment_count’,
‘numberposts’ => 5
);

以上になります。この他にも色々な指定方法がありますので、詳しくは関数リファレンス/WP Queryをご参照下さい。