サポート » 使い方全般 » 関連記事を現在のカスタムポストとタームに限定したい

  • 解決済 photographer hiro.

    (@mura0403)


    すみません。
    お力添えをお願いします。

    現在下のソースで関連記事をランダムに表示させています。

    <?php
      $args = array(
      'numberposts' => 6,
      'post_type' 	=> array($post_type), //投稿タイプの指定
      'orderby'     => 'rand',
      'post_status' => 'publish',
      'has_password' => false,//false or true
      'post__not_in'	=> array(get_the_ID()),//現在の記事を除外
      );
      $customPosts = get_posts($args);
      foreach($customPosts as $post) : setup_postdata( $post );
      ?>

    現在のカスタム投稿に絞り込むことはできたのですが
    カスタムタクソノミーの中にある現在所属するタームで絞り込むことは可能でしょうか。

    どうしてもうまく行きません。
    “blogcat”という名前でカスタムタクソノミーを作成しています。

    よろしくお願い致します。

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • モデレーター jim912

    (@jim912)

    トピック投稿者 photographer hiro.

    (@mura0403)

    jim912さんありがとうございます。

    'taxonomy' => 'people',
    			'field'    => 'slug',
    			'terms'    => 'bob',

    上のソースで’terms’の部分を自動取得したいのですがどう組めばよろしいでしょうか。
    singleページに書き込んでいます。

    モデレーター jim912

    (@jim912)

    記事のタームであれば、get_the_terms で取得可能です。

    トピック投稿者 photographer hiro.

    (@mura0403)

    jim912様
    どうもありがとうございました。
    無事できました。

    <?php
      $terms = get_the_terms($post->ID,'blogcat');
      foreach($terms as $term) {
      $term_slug = $term->slug;
      }
      $args = array(
      'numberposts' => 6,
      'post_type' 	=> array($post_type), //投稿タイプの指定
      'tax_query' => array(
    		array(
    			'taxonomy' => 'blogcat',
    			'field'    => 'slug',
    			'terms'    => $term_slug,
    		),
    	),
      'orderby'     => 'rand',
      'post_status' => 'publish',
      'has_password' => false,//false or true
      'post__not_in'	=> array(get_the_ID()),//現在の記事を除外
      );
      $customPosts = get_posts($args);
      foreach($customPosts as $post) : setup_postdata( $post );
      ?>

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「関連記事を現在のカスタムポストとタームに限定したい」には新たに返信することはできません。