サポート » 使い方全般 » カスタム投稿、特定カテゴリ一覧表示について

  • 解決済 tatsurou0102

    (@tatsurou0102)


    single.phpページにおいて、以下コードでカスタム投稿の特定のカテゴリを一覧で表示させたいのですが、「terms」の部分(「xxxx」)を表示しているページのページスラッグを代入したいのですが方法が分かりません。

    <?php
    $args = array(
        'post_type' => 'cast',
        'posts_per_page' => -1,
        'order' => 'DESC',
        'tax_query' => array(
          array(
            'taxonomy' => 'work_store',
            'terms' => array('xxxx'),
            'field' => 'slug'
          ),
        ),
      );
    $new_query = new WP_Query($args);
    ?>
    <?php if ($new_query->have_posts()) : ?>
4件の返信を表示中 - 1 - 4件目 (全4件中)
  • こんにちは

    ループ内の場合は、

    $post->post_name

    ループ外の場合は、

    get_post_field( 'post_name', get_post() )

    で、投稿のスラッグを取得することができます。

    トピック投稿者 tatsurou0102

    (@tatsurou0102)

    ishitakaさん
    ご返答ありがとうございます。

    以下のようにコードに入力したのですが、うまく表示されません。。。

    <?php
    $args = array(
        'post_type' => 'cast',
        'posts_per_page' => -1,
        'order' => 'DESC',
        'tax_query' => array(
          array(
            'taxonomy' => 'work_store',
            'terms' => array('$post->post_name'),
            'field' => 'slug'
          ),
        ),
      );
    $new_query = new WP_Query($args);
    ?>
    <?php if ($new_query->have_posts()) : ?>
    <?php
    $args = array(
        'post_type' => 'cast',
        'posts_per_page' => -1,
        'order' => 'DESC',
        'tax_query' => array(
          array(
            'taxonomy' => 'work_store',
            'terms' => array('get_post_field( 'post_name', get_post() )'),
            'field' => 'slug'
          ),
        ),
      );
    $new_query = new WP_Query($args);
    ?>
    <?php if ($new_query->have_posts()) : ?>

    恐縮なのですが、具体的なコードをご教授いただけないでしょうか。
    よろしくお願い致します。

    シングルクォーテーション(’)で括られていますね。

    'terms' => array( $post->post_name ),

    または

    'terms' => $post->post_name,
    トピック投稿者 tatsurou0102

    (@tatsurou0102)

    ishitakaさん
    ご返答ありがとうございます。

    無事意図した表示になりました!
    ご教授いただきありがとうございました。

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「カスタム投稿、特定カテゴリ一覧表示について」には新たに返信することはできません。