WordPress 投稿一覧(パラメーター)
投稿記事の一覧を5件分表示
<?php
$args = array(
'posts_per_page' => 5
);
$query = new WP_Query($args);
?>
<p>表示件数:<?php echo $query->post_count; ?>件</p>
<?php if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
PHP↑↑↑ 基本的な方法(表示件数の出力バージョン)
<?php
$args = array(
'posts_per_page' => 5, // 表示件数を5件に設定
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<?php if (has_post_thumbnail()) : ?>
<div>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<p><?php the_time('Y年m月d日') ?></p>
<p>TOPICS</p>
</div>
<?php endif; ?>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php
endwhile;
wp_reset_postdata();
endif;
?>PHP↑↑↑ ベースとしては、これで5件分の最新記事が表示されるよ
アイキャッチ:<?php if (has_post_thumbnail()) : ?>
投稿記事の日付を表示(出力)
<?php the_time('Y年m月d日') ?>・・・2025年10月10日
<?php the_time('Y/m/d'); ?>・・・・2025/10/10
<?php the_time('n月j日'); ?>・・・・10月10日PHP↑↑↑ 基本的な方法
<?php the_time('Y年n月j日(D)'); ?>PHP↑↑↑ 曜日付きで表示したい場合
<?php echo get_the_date('Y年n月j日(', get_the_ID()) .
['日','月','火','水','木','金','土'][get_post_time('w')] . ')'; ?>
PHP↑↑↑ 曜日を日本語にしたい場合
<p>投稿日:<?php echo get_the_time('Y年m月d日'); ?></p>PHP※ the_time() vs get_the_time()
・the_time() ……直接出力 する(echoされる)
・get_the_time() ……値を返す(変数に代入したい時に使う)