[Header]
指定カテゴリー内の記事一覧表示
WordPressのファイル「function.php」に下記を追加します。
function CategoryPostName($atts)
{
// 引数取得(デフォルト値の設定あり)
$atts=shortcode_atts(array("num"=>-1,"cat"=>"php"),$atts);
$arg = array(
'posts_per_page' => $atts['num'], // 表示件数
'orderby' => 'date', // 日付ソート
'order' => 'DESC', // 降順
'category_name' => $atts['cat'] // カテゴリースラッグ定
);
$posts = get_posts( $arg );
if( $posts )
{
$html = '<ul>';
foreach ( $posts as $p )
{
setup_postdata( $p );
$html .= '<li>';
$html .= '<a href="'.$p->guid.'">'.$p->post_title.'</a>';
$html .= '</li>';
}
wp_reset_postdata();
$html .= '</ul>';
}
return $html;
}
add_shortcode('ListName', 'CategoryPostName');
指定カテゴリ内の記事一覧を表示したい記事に下記を追記
引数を指定しない場合(デフォルトで、記事全件、カテゴリ名(スラッグ)が"php") [ListName] 引数を指定した場合(記事が4つ, カテゴリ名(スラッグ)が"php") [ListName num=4 cat="php"]
※カテゴリ名(スラッグ)は小文字になります。
以下のように指定カテゴリー内の記事がリスト表示されます。
- 指定カテゴリー内の記事1
- 指定カテゴリー内の記事2
- 指定カテゴリー内の記事3
- 指定カテゴリー内の記事4
[WordPressListName num=-1 cat=”wordpress”]
[Footer]
