Skip to content

Instantly share code, notes, and snippets.

@snoise
Last active December 20, 2015 17:48
Show Gist options
  • Save snoise/6170679 to your computer and use it in GitHub Desktop.
Save snoise/6170679 to your computer and use it in GitHub Desktop.
Wordpressのショートコード。特定のタグの記事一覧を表示する [myTagList tag="タグ名" order="ASC or DESC" count="表示数"]
<?php
//#######################
//ショートコードを作成:tagList
//特定のタグの記事一覧を表示する
//#######################
function shortCode_tagList($atts,$content = null ){
//ショートコードに定義した値を取得
extract( shortcode_atts( array(
'count' =>5, //表示数:
'order' => 'DESC' , //並び順:ASC/DESC,
'tag' => ''), $atts) ); //タグクラウド:タグ名
//条件定義
$args = array(
'posts_per_page'=>(isset($atts['count'])) ? sprintf(esc_html("%s"),$atts['count']) : 5,
'order' =>(isset($atts['order'])) ? sprintf(esc_html("%s"),$atts['order']) : "DESC",
'tag' =>sprintf(esc_html("%s"),$atts['tag']));
$tagPost =get_posts( $args );
//リスト表示
$list="<ul class='mytaglist_ul'>";
foreach($tagPost as $tag_post){
$list.="<li class='mytaglist_li'><a href='".get_permalink($tag_post->ID)."'>".$tag_post->post_title."</a></li>";
}
$list.="</ul>";
return $list;
}
add_shortcode('myTagList','shortCode_tagList');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment