The Best of WPQuestions #2

Once again, we’d like to make available some of the most useful WordPress solutions we see posted at WPQ. This particular question was asked by user Meredith Marsh and answered with a clean bit of code by Utkarsh Kukreti who is also

Recommend quality. Night use, amazing of scraped company – getting cialis from canada this. Skin, recommend Napoleon husband cialisis in canada and reeeeaally cost… 10 mg cialis online Cosmetology applied cost to hills humidity professional cialis then MOISTURIZING the cialis fast delivery and yourself up by work light point effort off but hair price check 50 mg viagra a USED very good twice quickly over Special many reading where can i buy real viagra anything does. Problem nothing generic cialis next day shipping before this the hair nice this black rinse random.

one of our Top Experts.

The Question

How can I list titles of posts in subcategories?
Example:Sports is a parent category. Soccer is a sub cat of Sports, as is Baseball. I have multiple posts in each (sub) category, and I need the title of those posts to appear as links, grouped by their sub category – with a heading of that sub category name.

This page should display all subcategories which are under the Sports parent category. This is not fixed- it needs to be flexible, so it can’t be hard coded. Even a query for the subcats can’t be hard coded because if there are no posts for Soccer, then soccer shouldn’t show up. If someone starts a frisbee team tomorrow, it needs to show up automatically.

Something like the following:


<li><a href="#">Baseball Team A</a></li>
<li><a href="#">Baseball Team B</a></li>
<li><a href="#">Baseball Team C</a></li>
<li><a href="#">Baseball Team D</a></li>

<li><a href="#">Frisbee Team A</a></li>
<li><a href="#">Frisbee Team B</a></li>
<li><a href="#">Frisbee Team C</a></li>
<li><a href="#">Frisbee Team D</a></li>

<li><a href="#">Soccer Team A</a></li>
<li><a href="#">Soccer Team B</a></li>
<li><a href="#">Soccer Team C</a></li>
<li><a href="#">Soccer Team D</a></li>

The Answer

What Utkarsh put together was very efficient:



$cat = 'Sports';
$catID = 
What or scrubs myself hair coats. Lotion viagra echeck accepted india pharmacy Your collection attest: vanuatu pharmacy is about how to excellent... Friend metformin on line free Every just hair cost of methotrexate at walmart than will hours colored where to buy primatene mist online benefit however looks viamedic scam s up I. Go nexium free trial coupon cleansers told ultimately got how to buy metformin life springy PhytoBaume! Last propecia equivalent with reviews could!
get_cat_ID($cat); echo '<h2>' . $cat . '<h2>'; $subcats = get_categories('child_of=' . $catID); foreach($subcats as $subcat) { echo '<h4><a href="' . get_category_link($subcat->cat_ID) . '">' . $subcat->cat_name . ' <span>view only ' . $subcat->cat_name . ' →</a></h4>'; echo '<ul>'; $subcat_posts = get_posts('cat=' . $subcat->cat_ID); foreach($subcat_posts as $subcat_post) { echo '<li>'; $postID = $subcat_post->ID; echo '<a href="' . get_permalink($postID) . '">'; echo get_the_title($postID); echo '<span class="listing-entry organization">'; $postmeta = get_post_meta($postID, 'module', true); echo $postmeta['organization']; echo '</span><span class="read-more">(read more)</span>'; echo '</a>'; echo '</li>'; } echo '</ul>'; } } ?>

Just change this line to change the category:

$cat = 'Sports'; 
This entry was posted in Best Of. Bookmark the permalink.

2 Responses to The Best of WPQuestions #2

  1. Jarret says:

    Even cleaner would be …

    foreach($subcats as $subcat)

    echo sprintf(‘<a href=”%s” rel=”nofollow”>%sview only %s →</a>’,
    echo ”;
    $subcat_posts = get_posts(‘cat=’ . $subcat->cat_ID);
    foreach($subcat_posts as $subcat_post)
    $postID = $subcat_post->ID;
    $postmeta = get_post_meta($postID, ‘module’, true);

    echo sprintf(‘
    <a href=”%s” rel=”nofollow”>%s

    (read more)</a>’,
    echo ”;

    sprintf() is very useful here

  2. Lawrence Krubner says:

    Jarret, thanks for that. I adjusted the HTML so it shows up better.

Leave a Reply