特定のカテゴリーの最新記事をトップページに表示する
foreach構文を勉強しています。
1.<?php global $_post;
$top_news = get_posts( array(
‘category_name’ => ‘info’, // カテゴリーのスラッグが’info’
‘posts_per_page’ =>5, // 1ページの表示件数が5件
));?>
get_posts()でデータを取得し、$top_newsに配列で格納します。
array 配列 同一の型のデータを一列に並べる
2.<?php foreach ( $top_news as $post ) : setup_postdata($post);?>
foreach ( $top_news as $post )
「$top_news」(記事の一覧)の内の1件の投稿記事を「$post」に渡しています。
ループ処理によって1件づつ$postに取り出すってこと。
setup_postdata($post)
ポストデータをセットアップする。「今この記事使いたいから〜とWordpressに教えています。」
つまり、the_time( ‘Y.m.d ‘)やthe_title()などの関数によって表示できる用意をするということ。
the_time( ‘Y.m.d ‘)やthe_title()などの関数は「用意ドン!」の「ドン!」ということと考えればよい。