ウェブサイト実験
Web勉強中おじさん
STORKテーマで使える任意のカテゴリを指定して最新記事を表示させるショートコード
任意のカテゴリslugを設定して、指定の数だけ最新記事を表示させるショートコード(タグでの絞り込み機能付き)
■ 使い方
記法:[articles num=”” cat=”” tag=””]
■ オプション
num:表示記事数(省略可) - 省略した場合、全件取得して表示します
cat:カテゴリー名(slug・省略不可)
tag:タグ名(slug・省略可・カンマ区切りで複数選択可) - 設定されたタグを含む投稿に絞り込みます
// 指定カテゴリ記事取得引用ショートコード
function get_articles($attr) {
//引数の取り出し
if(isset($attr['num'])){
$num = $attr['num'];
$num =(int)$num;
}else{
$num = -1;
}
if(isset($attr['tag'])){
$tag = $attr['tag'];
}else{
$tag = '';
}
if(isset($attr['cat'])){
// 指定カテゴリのカテゴリIDを取得
$catInfo = get_category_by_slug($attr['cat']);
$catId = $catInfo->cat_ID;
}else{
$catId = '';
}
// 表示内容格納用変数
$display = '';
// タグの有無で分岐
if($tag != ''){
// 取得する投稿の条件
$args = array(
'posts_per_page' => $num,
'category' => $catId,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $tag
)
)
);
}else{
// 取得する投稿の条件
$args = array(
'posts_per_page' => $num,
'category' => $catId,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
}
// 投稿を取得
$selectedPosts = get_posts($args);
if(!empty($selectedPosts)){
if(is_mobile()){
$display .= '<div class="top-post-list">'."\n";
// 取得した投稿分繰り返し
foreach($selectedPosts as $selectedPost){
$postLink = get_permalink($selectedPost->ID);
$thumbLink = get_the_post_thumbnail_url( $selectedPost->ID, 'medium' );
$autorData = get_userdata($selectedPost->post_author);
$display .= '<article class="post-list animated fadeIn" role="article">'."\n";
$display .= '<a href="' .$postLink .'" rel="bookmark" title="' . $selectedPost->post_title . '" class="cf no-icon" sl-processed="1">'."\n";
$display .= '<figure class="eyecatch">'."\n";
$display .= get_the_post_thumbnail($selectedPost->ID, 'home-thum');
$display .= '<span class="cat-name cat-id-' . $catId . '">' . $catInfo->cat_name . '</span>'."\n";
$display .= '</figure>'."\n";
$display .= '<section class="entry-content">'."\n";
$display .= '<h1 class="h2 entry-title">' . $selectedPost->post_title . '</h1>'."\n";
$display .= '<p class="byline entry-meta vcard">'."\n";
$display .= '<span class="date gf updated">' . mysql2date('Y/n/j', $selectedPost->post_date) . '</span>'."\n";
$display .= '<span class="writer name author"><span class="fn">' . $authorData->user_nicename . '</span></span>'."\n";
$display .= '</p>'."\n";
$display .= '</section>'."\n";
$display .= '</a>'."\n";
$display .= '</article>'."\n";
}
}else{
$display .= '<div class="post-list-card cf">'."\n";
// 取得した投稿分繰り返し
foreach($selectedPosts as $selectedPost){
$postLink = get_permalink($selectedPost->ID);
$thumbLink = get_the_post_thumbnail_url( $selectedPost->ID, 'medium' );
$autorData = get_userdata($selectedPost->post_author);
$display .= '<article class="post-list cf animated fadeIn" role="article">'."\n";
$display .= '<a href="' .$postLink .'" rel="bookmark" title="' . $selectedPost->post_title . '">'."\n";
$display .= '<figure class="eyecatch">'."\n";
$display .= get_the_post_thumbnail($selectedPost->ID, 'home-thum');
$display .= '<span class="cat-name cat-id-' . $catId . '">' . $catInfo->cat_name . '</span>'."\n";
$display .= '</figure>'."\n";
$display .= '<section class="entry-content cf">'."\n";
$display .= '<h1 class="h2 entry-title">' . $selectedPost->post_title . '</h1>'."\n";
$display .= '<p class="byline entry-meta vcard">'."\n";
$display .= '<span class="date gf updated">' . mysql2date('Y/n/j', $selectedPost->post_date) . '</span>'."\n";
$display .= '<span class="writer name author"><span class="fn">' . $authorData->user_nicename . '</span></span>'."\n";
$display .= '</p>'."\n";
$display .= '<div class="description">' . $selectedPost->post_excerpt . '</div>'."\n";
$display .= '</section>'."\n";
$display .= '</a>'."\n";
$display .= '</article>'."\n";
}
}
$display .= '</div>'."\n";
}else{
$display .= '<p>記事がありません</p>';
}
return $display;
}
add_shortcode('articles', 'get_articles');
The following two tabs change content below.
最近のコメント