Quote through wordpress categories

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

  • cycle through all categories
  • highlight the category name (with reference to
  • echo out last 5 posts in this category (with permanent link for publication)

For each html there will 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

+7
wordpress wordpress-theming
source share
4 answers

Sorry, missed that you need 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 ?> 
+8
source share

Keeping things is just here, how can you solve it

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

I made this bit of code to scroll through 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><? } } 
+1
source share

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 it works like a charm.

Remember to configure the arguments to display only 5 messages, not all.

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

Add 'showposts' => 5 to your current array of arguments in a loop that repeats through messages in each category.

0
source share

All Articles