Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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