function getCatItems($atts, $content = null) {
extract(shortcode_atts(array(
"num" => '2',
"cat" => '12'
), $atts));
// 処理中のpost変数をoldpost変数に退避
global $post;
$oldpost = $post;
// カテゴリーの記事データ取得
$myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
$newDispDays = 7 * 24 * 60 * 60; // 「New」を表示する日数(公開日から7日間)
$today = date_i18n('U'); // 本日の日付の秒数
if($myposts) {
// 記事がある場合↓
$retHtml = '<div class="getPostDispArea">';
// 取得した記事の個数分繰り返す
foreach($myposts as $post) :
// 投稿ごとの区切りのdiv
$retHtml .= '<div class="getPost">';
// 記事オブジェクトの整形
setup_postdata($post);
// サムネイルの有無チェック
if ( has_post_thumbnail() ) {
// サムネイルがある場合↓
$retHtml .= '<div class="getPostImgArea">' . get_the_post_thumbnail($page->ID, 'thumbnail') . '</div>';
} else {
// サムネイルがない場合↓※何も表示しない
$retHtml .= '';
}
// 文章のみのエリアをdivで囲う
$retHtml .= '<div class="getPostStringArea">';
// 投稿年月日を取得
$year = get_the_time('Y'); // 年
$month = get_the_time('n'); // 月
$day = get_the_time('j'); // 日
$retHtml .= '<span><font color=#ff0000;">投稿日</font>:[' . $year . '年' . $month . '月' . $day . '日] </span>';
// 投稿日の秒数を取得
$entryDay = get_the_time('U');
// 経過日数を取得
$elapsedDays = $today - $entryDay;
// タイトル設定(リンクも設定する)
$retHtml.= '<h4 class="title">';
// 7日経過していない?
if( $newDispDays > $elapsedDays ){
$retHtml.= '<span style="color:red">NEW! </span>';
}
// タイトル設定(リンクも設定する)
$retHtml.= '<span class="getPostTitle">';
$retHtml.= '<a href="' . get_permalink() . '">' . the_title("","",false) . '</a>';
$retHtml.= '</span></h4>';
// 本文を抜粋して取得
$getString = get_the_excerpt();
$retHtml.= '<div class="getPostContent"><font color="#339966">' . $getString . '</font></div>';
$retHtml.= '</div></div>';
endforeach;
$retHtml.= '</div>';
} else {
// 記事がない場合↓
$retHtml='<p>記事がありません。</p>';
}
// oldpost変数をpost変数に戻す
$post = $oldpost;
return $retHtml;
}
// 呼び出しの指定
add_shortcode("getCategoryArticle", "getCatItems");
-------------
*Shortcode: 投稿・固定ページへ
[getCategoryArticle] [getCategoryArticle num="1" cat="5"]