// 一覧記事取得関数 ショートコードで呼び出し // 呼び出し元での指定も可能 -> [getCategoryArticle num="x" cat="y"] function getCatItems($atts, $content = null) { extract(shortcode_atts(array( "num" => '2', "cat" => '12' ), $atts)); // 処理中のpost変数をoldpost変数に退避 //global $post; //$oldpost = $post; // カテゴリーの記事データ取得 $args = array( 'post_type'=>'post', 'posts_per_page' => $num, 'cat' => $cat, 'orderby' => 'post_date', 'order' => 'DESC', ); $wp_query = new WP_Query( $args ); // ループ $retHtml =""; if ( $wp_query->have_posts() ) { while ( $wp_query->have_posts() ) { $wp_query->the_post(); ob_start(); get_template_part( 'template-parts/post/loop','post' ); $retHtml .= ob_get_contents(); ob_end_clean(); } } return $retHtml; } // 呼び出しの指定 add_shortcode("getCategoryArticle", "getCatItems");固定記事などでショートコードで呼び出し
[getCategoryArticle num="5" cat="15"]
コメント