9

私はWordpressを初めて使用し、カテゴリループを作成しようとして髪を引っ張っています. ループは次のようになっています。

  1. すべてのカテゴリをループする
  2. カテゴリ名をエコーアウトします(へのリンク付き)
  3. そのカテゴリの最後の 5 つの投稿をエコーアウトします (投稿へのパーマリンク付き)

それぞれのhtmlは次のようになります

<div class="cat_wrap">
   <div class="cat_name">
       <a href="<?php get_category_link( $category_id ); ?>">Cat Name</a>
   </div>
   <ul class="cat_items">
      <li class="cat_item">
         <a href="permalink">cat item 1</a>
      </li>
      <li class="cat_item">
         <a href="permalink">cat item 2</a>
      </li>
      <li class="cat_item">
          <a href="permalink">cat item 3</a>
      </li>
      <li class="cat_item">
         <a href="permalink">cat item 4</a>
      </li>
      <li class="cat_item">
         <a href="permalink">cat item 5</a>
      </li>
   </ul>
</div>

助けてください

4

4 に答える 4

12

おっと、あなたが5つの投稿を望んでいたことを逃しました

<?php
//for each category, show 5 posts
$cat_args=array(
  'orderby' => 'name',
  'order' => 'ASC'
   );
$categories=get_categories($cat_args);
  foreach($categories as $category) { 
    $args=array(
      'showposts' => 5,
      'category__in' => array($category->term_id),
      'caller_get_posts'=>1
    );
    $posts=get_posts($args);
      if ($posts) {
        echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        foreach($posts as $post) {
          setup_postdata($post); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>
于 2009-11-24T12:55:58.177 に答える
6

ここで物事をシンプルに保つことで、問題を解決できます

<?php wp_list_categories('show_count=1&title_li=<h2>Categories</h2>'); ?>
于 2009-11-22T23:40:52.703 に答える
0

この他の Stackoverflow スレッドを見てください。

https://wordpress.stackexchange.com/questions/346/loop-through-custom-taxonomies-and-display-posts/233948#233948

私は生産で使用し、魅力のように機能する回答を投稿しました。

すべてではなく、5 つの投稿のみを表示するように引数を調整することを忘れないでください。

$args = array( 'showposts' => 5 );

'showposts' => 5 を、各カテゴリの投稿を繰り返すループ内の現在の引数の配列に追加します。

于 2016-08-03T18:32:59.807 に答える