Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save senooat/7bca95a94ca2e7fc4ee72839aecec610 to your computer and use it in GitHub Desktop.
Save senooat/7bca95a94ca2e7fc4ee72839aecec610 to your computer and use it in GitHub Desktop.
/*-----------------------------------
アイキャッチ付き
News表示ショートコード
[news num="5"]
-----------------------------------*/
function getNewsItems($atts) {
extract(shortcode_atts(array(
"num" => '', //最新記事リストの取得数
), $atts));
global $post;
$cat_id = get_category_by_slug('news')->cat_ID;
$myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat_id);
if($myposts){
$retHtml.='<ul class="news__list">';
$catlink = get_category_link($cat_id);
foreach($myposts as $post) :
setup_postdata($post);
//アイキャッチ画像(150)
$image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), "thumbnail" );//thumbnail, medium, large, full
list( $src, $width, $height ) = $image;
if($src):
$thumb = '<div class="thumb"><a href="'.$entry_permalink.'"><img src="'.$src.'" width="80" alt="" class="responsive" /></a></div>';
else:
$thumb = '<div class="thumb"><a href="'.$entry_permalink.'"><img src="'.IMG150150.'" width="80" alt="" class="responsive" /></a></div>';
endif;
$retHtml.='<li><a href="'.get_permalink().'">';
$retHtml.= $thumb;
$retHtml.='<time datetime="'.get_the_time('Y-m-d').'">'.get_post_time( get_option( 'date_format' )).'</time>';
$retHtml.='<span class="ttl">'.the_title("","",false).'</span>';
$retHtml.='</a></li>';
endforeach;
$retHtml.='</ul>';
$retHtml.='<a class="more" href="'.$catlink.'">お知らせ一覧へ</a>';
wp_reset_postdata();
}else{
$retHtml='<p class="none">投稿がありません。</p>';
}
return $retHtml;
}
add_shortcode("news", "getNewsItems");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment