[getCategoryArticle num=”x” cat=”y”]
function getCatItems($atts, $content = null) {
extract(shortcode_atts(array(
“num” => ‘3’,
“cat” => ’13’
), $atts));
// 処理中のpost変数をoldpost変数に退避
global $post;
$oldpost = $post;
// カテゴリーの記事データ取得
$myposts = get_posts(‘numberposts=’.$num.’&order=DESC&orderby=post_date&category=’.$cat);
if($myposts) {
// 記事がある場合↓
$retHtml = ‘
// 取得した記事の個数分繰り返す
foreach($myposts as $post) :
// 投稿ごとの区切りのdiv
$retHtml .= ‘
// 記事オブジェクトの整形
setup_postdata($post);
// サムネイルの有無チェック
if ( has_post_thumbnail() ) {
// サムネイルがある場合↓
$retHtml .= ‘
‘;
} else {
// サムネイルがない場合↓※何も表示しない
$retHtml .= ”;
}
// 文章のみのエリアをdivで囲う
$retHtml .= ‘
// 投稿年月日を取得
$year = get_the_time(‘Y’); // 年
$month = get_the_time(‘n’); // 月
$day = get_the_time(‘j’); // 日
$retHtml .= ‘この記事は’ . $year . ‘年’ . $month . ‘月’ . $day . ‘日に投稿されました‘;
// タイトル設定(リンクも設定する)
$retHtml.= ‘
‘;
$retHtml.= ‘‘ . the_title(“”,””,false) . ‘‘;
$retHtml.= ‘
‘;
// 本文を抜粋して取得
$getString = get_the_excerpt();
$retHtml.= ‘
‘;
$retHtml.= ‘
‘;
endforeach;
$retHtml.= ‘
‘;
} else {
// 記事がない場合↓
$retHtml=’
記事がありません。
‘;
}
// oldpost変数をpost変数に戻す
$post = $oldpost;
return $retHtml;
}
// 呼び出しの指定
add_shortcode(“getCategoryArticle”, “getCatItems”);
?>