循环遍历 wordpress 类别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1780386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:33:05  来源:igfitidea点击:

Looping through wordpress categories

wordpresswordpress-theming

提问by Simon

I am new to Wordpress and been pulling my hair out trying to create a category loop. The loop is supposed to:

我是 Wordpress 的新手,一直在努力创建类别循环。该循环应该:

  1. loop through all categories
  2. echo out the category name (with link to
  3. echo out the last 5 posts in that category (with permalink to post)
  1. 遍历所有类别
  2. 回显类别名称(带有链接到
  3. 回显该类别中的最后 5 个帖子(带有要发布的永久链接)

The html for each would be

每个的 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>

Please help

请帮忙

回答by Michael

Oops, missed that you wanted 5 posts

糟糕,错过了你想要的 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
?>

回答by streetparade

Hy keeping things simple here is how you can solve it

在这里保持简单是解决问题的方法

<?php wp_list_categories('show_count=1&title_li=<h2>Categories</h2>'); ?>

回答by Etienne Dupuis

I have made this bit of code to loop through the nested categories. Sharing.

我已经制作了这段代码来循环遍历嵌套类别。分享。

        //Start on the category of your choice       
        ShowCategories(0);

        function ShowCategories($parent_category) {
                $categories = get_categories(array('parent' => $parent_category, 'hide_empty' => 0));  
                foreach ($categories as $category) {
                    ?><ul><li><?=$category->cat_name;?><?
                    ShowCategories($category->cat_ID);
                    ?></li></ul><?
                }
        }

回答by drjorgepolanco

Take a look at this other Stackoverflow thread:

看看另一个 Stackoverflow 线程:

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

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

I posted an answer that I use in production and works like a charm.

我发布了一个我在生产中使用的答案,并且效果很好。

Just remember to adjust the arguments to display only 5 posts, instead of all.

请记住调整参数以仅显示 5 个帖子,而不是全部。

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

Add 'showposts' => 5 to your current array of arguments in the loop that iterates through the posts of each category.

将 'showposts' => 5 添加到循环中的当前参数数组,循环遍历每个类别的帖子。