Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save senooat/bdaaa01b31ab28dcef56db3809517008 to your computer and use it in GitHub Desktop.
Save senooat/bdaaa01b31ab28dcef56db3809517008 to your computer and use it in GitHub Desktop.
/*-----------------------------------
お知らせ表示ショートコード
[news num="5" cat="news" showcat=""]
-----------------------------------*/
function getNewItems($atts) {
extract(shortcode_atts(array(
"num" => '', //最新記事リストの取得数
"cat" => '', //表示する記事のカテゴリー指定
"showcat" => '' //カテゴリー表示の有無
), $atts));
global $post;
$oldpost = $post;
$cat_id = get_category_by_slug($cat)->cat_ID;
$myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat_id);
if($myposts){
$retHtml='<div class="news">';
$retHtml.='<ul class="news__list">';
$catlink = get_category_link($cat_id);
foreach($myposts as $post) :
$cat = get_the_category();
$catname = $cat[0]->cat_name;
$catslug = $cat[0]->slug;
setup_postdata($post);
$retHtml.='<li>';
$retHtml.='<time>'.get_post_time( get_option( 'date_format' )).'</time>';
if($showcat)$retHtml.='<span class="cat '.$catslug.'">'.$catname.'</span>';
$retHtml.='<span class="ttl"><a href="'.get_permalink().'">'.the_title("","",false).'</a></span>';
$retHtml.='</li>';
endforeach;
$retHtml.='</ul>';
$retHtml.='<p class="more"><a href="'.$catlink.'">≫過去掲載一覧へ</a></p>';
$retHtml.='</div>';
$post = $oldpost;
wp_reset_postdata();
}else{
$retHtml='<p class="none">投稿がありません。</p>';
}
return $retHtml;
}
add_shortcode("news", "getNewItems");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment