サポート » 使い方全般 » 1つの記事が属する全てのカテゴリの全ての記事の抜粋

  • 解決済 tomoyankees

    (@tomoyankees)


    一つの記事を表示した時に、その記事に続いてその記事が属する全てのカテゴリの全ての記事の抜粋が表示されるようsingle.phpにコードを組んでいるのですがいまいち上手くいきません。

    見た目はこうなる予定です。

    ———————————–
    本文

    <h2>記事の所属するカテゴリー1</h2>
    -抜粋1
    -抜粋2
    -抜粋3

    <h2>記事の所属するカテゴリー2</h2>
    -抜粋4
    -抜粋5
    -抜粋6
    -抜粋7
    ————————————

    一応組んでみたコードですが上手く機能しません。

    <?php $categories = get_the_category();
    foreach($categories as $category):
    $related_posts = get_posts(‘numberposts=-1”category=’.$category->cat_ID.’&exclude=’.$post->ID);
    if($related_posts): ?>
    <h2><?php echo $category->cat_name; ?> カテゴリの他の記事h2>
    <?php foreach($related_posts as $related_post): ?>
    <div id=”entry<?php get_the_ID($related_post); ?>”>
    <?php get_the_excerpt($related_post); ?>
    </div>
    <?php endforeach; ?>
    <?php endif; endforeach; ?>

    どなたかこのコードを直せる方がよろしくお願いします。

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • <?php
    $categories = get_the_category();
    foreach($categories as $category) :
      $args = array(
        'cat' => $category->cat_ID,
        'showposts' => -1,
        'post__not_in' => array($post->ID)
      );
      query_posts($args);
      if(have_posts()) :
        echo '<h2>' . $category->cat_name . 'カテゴリの他の記事</h2>';
        while(have_posts()) :
          the_post();
          echo '<div id="entry-' . get_the_ID() . '">';
          the_excerpt();
          echo '</div>';
        endwhile;
      endif;
      wp_reset_query();
    endforeach;
    ?>

    でどうでしょう。

    トピック投稿者 tomoyankees

    (@tomoyankees)

    >kz様

    ありがとうございます!
    試してみますので少々お待ちください。

    トピック投稿者 tomoyankees

    (@tomoyankees)

    >kz様

    完璧でした。
    思い通りの結果が表示されました。
    10時間くらい必死でネットで探していた自分が何なのかというくらい完璧でした。

    これで安心してショムニの再放送が見られます(笑)

    本当にありがとうございました。

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • トピック「1つの記事が属する全てのカテゴリの全ての記事の抜粋」には新たに返信することはできません。