複数の投稿タイプの記事を1つの一覧ページに表示させる

複数の投稿タイプの記事一覧。投稿日と記事タイトルのほかに、投稿タイプ名、カテゴリ名を表示。

ページャーなし…最新数件だけ

<?php
$args = array(
    'posts_per_page' => 5,
    'post_type' => array('post','demo'), //postは通常の投稿機能
    'orderby' => 'date',
);
$my_posts = get_posts($args);

foreach ($my_posts as $post): setup_postdata($post);
$obj = get_post_type_object( $post->post_type ); 
    ?>
    <tr>
        <th><?php the_time('Y.n.d'); ?><span class="cat_label type_<?php echo $post->post_type; ?>"><?php echo $obj->labels->name;?></span></th>
        <td><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></td>
    </tr>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

投稿タイプごとに、投稿タイプ名を色の異なるラベルのように表示したかったので、class名に $post->post_type; をechoしている。

ページャーありのアーカイブページとして

<?php
    $wp_query = new WP_Query();
    $my_posts = array(
        'post_type' => array('demo', 'post'),//表示したい投稿タイプ
        'posts_per_page'=> '2',
        'paged' => $paged,
    );
    $wp_query->query( $my_posts );
    if( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post();
    $obj = get_post_type_object( $post->post_type ); 
?>
<div class="list">
    <div class="list-item">
        <h2>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </h2>
        <p class="date"><?php the_time('Y.m.d') ?></p>
        <p class="type">
            <span class="cat_label type_<?php echo $post->post_type; ?>"><?php echo $obj->labels->name;?></span>:
            <?php 
            the_taxonomies(array(
                'template' => '%2$l'//タクソノミー名は表示しない,
                'term_template' => '<span class="tag">%2$s</span>',))//リンクは表示しない;
            ?>

        </p>
    </div>
</div>
    
<?php endwhile; endif; wp_reset_postdata(); ?>
<?php wp_pagination();//ページネーション ?>

ページネーションは以下のfunctions.phpで登録したもの。設定してないと動かない。

// ページネーション表示
function wp_pagination()
{
    global $wp_query;
    $big = 999999999;
    echo paginate_links(array(
        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
        'format' => '?paged=%#%',
        'current' => max(1, get_query_var('paged')),
        'prev_text' => '<span>≪</span>',
        'next_text' => '<span>≫</span>',
        'total' => $wp_query->max_num_pages
    ));
}
add_action('init', 'wp_pagination');

the_taxonomies();の部分は表示カスタマイズ可能なので詳細解説記事をかくにんされたし