なるほどねっ!MEMO BLOG

WEBのことWordPressのこと...いろいろ。なるほどねっ!と思ったことをメモしております!

(WP)TOPページに投稿一覧を表示!

デザインは個々にするとして、とりあえずTOPページに投稿一覧を表示してみる。

 

★普通の投稿を表示する場合↓

<?php if(have_posts()): while(have_posts()):the_post(); ?>

<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

<time datetime="<?php the_time('Y-m-d'); ?>"><?php the_time('Y.m.d'); ?></time>
<p><?php the_category(', '); ?></p>
<p><?php the_content(); ?></p>

<?php endwhile; endif; ?>

 

※<?php the_content(); ?>を<?php the_excerpt(); ?>をにすると

記事本文の先頭から110文字移行の文末が[…]になります。

 

 

★投稿タイプ(CPT UI)を表示する場合↓

<?php $args = array(
'numberposts' => 5,
'post_type' => 'custom'
);
$customPosts = get_posts($args);
if($customPosts) : foreach($customPosts as $post) : setup_postdata( $post ); ?>

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
<?php else : ?>
<p>現在、ご紹介できる記事はございません。</p>
<?php endif;
wp_reset_postdata(); //クエリのリセット ?>

 

※5は表示する記事の数で、customは投稿タイプ名です。

条件を追加する場合はその下に続けます!

<?php else : ?>以下は、記事が無い場合に入れる文言となります。

 

 

以上!!