投稿ページにショートコードを使って
カテゴリの記事一覧やタグ一覧を表示させたい
function.phpに
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | function cat_tag_list_func( $atts ){ $str = '<ul>' ; global $post ; $atts = shortcode_atts( array ( 'tag' => '' , 'cat' => '' , 'num' => '' ), $atts ); $arg = array ( 'posts_per_page' => $atts [ 'num' ], 'orderby' => 'date' , 'order' => 'DESC' , 'tag' => $atts [ 'tag' ], 'category_name' => $atts [ 'cat' ] ); $posts = get_posts( $arg ); foreach ( $posts as $post ): setup_postdata( $post ); $str .= '<li>' ; $str .= '<a href="' .get_permalink(). '">' .the_title( '' , '' ,false). '</a>' ; $str .= '</li>' ; endforeach ; $str .= '</ul>' ; wp_reset_postdata(); return $str ; } add_shortcode( 'cat_tag_list' , 'cat_tag_list_func' ); |
投稿ページに下記を書く
タグの場合
1 | [cat_tag_list num=表示数 tag=タグのスラッグ] |
ex.
1 | [cat_tag_list num=100 tag=tagname] |
カテゴリの場合
1 | [cat_tag_list num=表示数 cat=カテゴリ名] |
ex.
1 | [cat_tag_list num=100 cat=category] |