ファンクションへ
オリジナル関数だと思う
//特定のカテゴリと特定のタグで絞った記事数の表示方法 function taxonomy_found_posts_num() { $args = array( 'post_type' => array('shoku','asobu','share','seikatsu','iryou','manabu','koukyou','ofuroyado'), //カスタム投稿名 ); $my_query = new WP_Query($args); return $my_query->found_posts; } //表示箇所に //<?php echo taxonomy_found_posts_num('hokkaido','タグ1'); 件<br /> //<?php echo taxonomy_found_posts_num('aomori','タグ2'); 件
上記正式コード
<?php function taxonomy_found_posts_num($cat_slug,$tag_slug) { $args = array( 'post_type' =>'post', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => $cat_slug, ), array( 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => $tag_slug, ), ), 'posts_per_page' => 1 ); $my_query = new WP_Query($args); return $my_query->found_posts; } ?>
<?php //特定のポストタイプと特定のユーザーIDで絞った記事数の表示方法 function taxonomy_found_posts_num1($post_type,$user_id) { $args = array( 'post_type' => $post_type, //カスタム投稿名 'author' => $user_id, ); $my_query = new WP_Query($args); return $my_query->found_posts; } //ログインユーザーのIDを取得 $user = wp_get_current_user(); $user_idd = $user->get('ID')."\n"; // ID //出力 echo taxonomy_found_posts_num1('shoku',$user_idd); ?>