English 中文(简体)
Looping through wordpress categories
原标题:

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

  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)

The html for each would be

<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

问题回答

Oops, missed that you wanted 5 posts

<?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
?>

Hy keeping things simple here is how you can solve it

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

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><?
                }
        }

Take a look at this other Stackoverflow thread:

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.

$args = array(  showposts  => 5 );

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





相关问题
Wrap stray text in <p> tags

Wordpress issue.. how do I wrap stray text in P tags? Example: Before- <div class = "content"> <img src = "hello.jpg"/> <h1>Introduction</h1> Hello! this is ...

Using jQuery Plugins with Wordpress

Having a bit of trouble using jQuery plugins (Superfish, jQuery UI, etc) using Wordpress. Everything works fine in my plain non-Wordpress site, but Wordpress seems to conflict with JQuery. There must ...

WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Why can t I properly style a blockquote in Wordpress?

On the design I just created for my website, I have a blockquote styled with two quote images using the span technique in css: blockquote { background-image: url(images/openquote.jpg); background-...

How does the WordPress <!--nextpage--> tag actually work?

What happens? I m guessing that somehow the post or page is parsed before displaying, and then just split into two methods? I can t seem to find any documentation on how the underlying <?php ...

Wordpress Plug-ins: How-to add custom URL Handles

I m trying to write a Wordpress Plug-in but can t seem to figure out how you would modify how a URL gets handled, so for example: any requests made for: <url>/?myplugin=<pageID> will ...

热门标签