function.phpに
function show_Cat_Posts_func($atts) {
global $post;
$output = "";
extract(shortcode_atts(array(
'cat' => 1, // デフォルトカテゴリーID = 1
'show' => 3 // デフォルト表示件数 = 3
), $atts));
$cat = rtrim($cat, ",");
// get_postsで指定カテゴリーの記事を指定件数取得
$args = array(
'cat' => $cat,
'posts_per_page' => $show
);
$my_posts = get_posts($args);
// 上記条件の投稿があるなら$outputに出力、マークアップはお好みで
if ($my_posts) {
// カテゴリーを配列に
$cat = explode(",", $cat);
$catnames = "";
foreach ($cat as $catID) : // カテゴリー名取得ループ
$catnames .= get_the_category_by_ID($catID).", ";
endforeach;
$catnames = rtrim($catnames, ", ");
$output .= '<aside class="showcatposts">'."\n";
$output .= '<h2 class="showcatposts-title">カテゴリー「'.$catnames.'」'."の最新記事(".$show."件)</h2>\n";
$output .= '<ul class="showcatposts-list">'."\n";
foreach ($my_posts as $post) : // ループスタート
setup_postdata($post); // get_the_title() などのテンプレートタグを使えるようにする
$output .= '<li id="post-'.get_the_ID().'" '.get_post_class().'><a href="'.get_permalink().'">'.get_the_title()."</a></li>\n";
endforeach; // ループ終わり
$output .= "</ul>\n";
$output .= "</aside>\n";
}
// クエリのリセット
wp_reset_postdata();
return $output;
}
add_shortcode('showcatposts', 'show_Cat_Posts_func');
投稿の編集画面に
[showcatposts cat="1" show="3"]
<参考>
http://gatespace.jp/2012/03/28/show_categoryposts_shortcode/
http://coliss.com/articles/blog/wordpress/wordpress-shortcodes.html