【Wordpress】新着記事の表示方法 | Webウォーカーのブログ

Webウォーカーのブログ

ブログの説明を入力します。

<ul>
<?php query_posts('posts_per_page=5&cat=4'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"> <span class="date">
<?php the_time('Y年n月j日'); ?>
<?php
$days=30;
$today=date('U'); $entry=get_the_time('U');
$diff1=date('U',($today - $entry))/86400;
?>
</span>
<?php the_title();?></a>
</li>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
</ul>



◆投稿のカテゴリ指定表示◆

「posts_per_page=5」は最新5件表示ってこと

「cat=4」は投稿のカテゴリID4の記事を表示ってこと





◆カスタム投稿の場合◆

「cat=4」を「post_type=info」とかに変更

「post_type」はカスタム投稿のこと

「info」は自分で付けたカスタム投稿名



◆同じ意味でこんな書き方も出来る↓◆


<?php
$args = array(
'post_type' => 'info', // 表示するカスタム投稿名
'posts_per_page' => 5, // 表示する件数
);
?>


<ul>
<?php query_posts( $args ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"> <span class="date">
<?php the_time('Y年n月j日'); ?>
<?php
$days=30;
$today=date('U'); $entry=get_the_time('U');
$diff1=date('U',($today - $entry))/86400;
?>
</span>
<?php the_title();?></a>
</li>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
</ul>