投稿記事を古い記事から表示させるとき
array_reverse()
<?php
global $post;
$args = array('posts_per_page' => 5);//記事5つ分の内容を配列の形で格納
$argsreverse = array_reverse($args);//配列を逆順にする
$myposts = get_posts($argsreverse);
foreach ($myposts as $post) {
setup_postdata($post);
?>
<a class="news-item" href="<?php the_permalink(); //記事のリンクを表示
?>">
<div class="news-item-img">
<?php
if (has_post_thumbnail()) {
// アイキャッチ画像が設定されてれば大サイズで表示
the_post_thumbnail('large');
} else {
// なければnoimage画像をデフォルトで表示
echo '<img src="' . esc_url(get_template_directory_uri()) . '/img/noimg.png" alt="">';
}
?>
</div>
<div class="news-item-body">
<time class="news-item-published" datetime="<?php the_time('c'); ?>"><?php the_time('Y/n/j'); ?></time>
<h3 class="news-item-title"><?php the_title(); //タイトルを表示
?></h3>
</div><!-- /news-item-body -->
</a><!-- /news-item -->
<?php
}
wp_reset_postdata();
?>
この記事をシェアする
タグ

