Last active
December 19, 2015 12:19
-
-
Save wokamoto/5954406 to your computer and use it in GitHub Desktop.
[WordPress] shortcode recent_posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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