Skip to content

Instantly share code, notes, and snippets.

@wokamoto
Last active December 19, 2015 12:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wokamoto/5954406 to your computer and use it in GitHub Desktop.
Save wokamoto/5954406 to your computer and use it in GitHub Desktop.
[WordPress] shortcode recent_posts
<?php
add_shortcode('recent_posts', 'my_recent_posts');
function my_recent_posts($atts) {
// デフォルトテンプレート
$template = '<div style="border-bottom:dotted 1px #aaaaaa;margin-bottom:20px;font-size:14px">
<div class="title"><a href="%s" target="_top">%s</a></div>
<div class="day" style="font-size:12px;color:#999999">%s</div>
</div>
';
// 引数の処理
extract(shortcode_atts(array(
'template' => $template,
'args' => 'post_type=post&posts_per_page=10',
), $atts));
$the_list = '';
// 最新のポスト取得
$posts = get_posts($args);
foreach ($posts as $post) {
$the_list .= sprintf(
$template,
esc_attr(get_permalink($post->ID)),
esc_html($post->post_title),
mysql2date("Y年m月j日", $post->post_date)
);
}
return $the_list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment