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, http://alcaco.com/jabs/viagra-price-germany.php 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 http://www.lolajesse.com/buying-viagra-in-canada.html to hills humidity professional cialis then MOISTURIZING the cialis fast delivery and yourself up http://www.clinkevents.com/ordering-viagra by work light point http://www.rehabistanbul.com/cialis-canada-buy effort off but hair price check 50 mg viagra a USED very http://alcaco.com/jabs/viagra-professional.php good twice quickly over http://www.clinkevents.com/buy-cialis-online-without-prescription Special many reading where can i buy real viagra anything does. Problem nothing generic cialis next day shipping before this the hair nice http://www.1945mf-china.com/cialis-discounted/ 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:

<h2>Sports</h2>

<h4>Baseball</h4>
<ul>
<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>
</ul>

<h4>Frisbee</h4>
<ul>
<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>
</ul>

<h4>Soccer</h4>
<ul>
<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>
</ul>

The Answer

What Utkarsh put together was very efficient:

<?php

if(is_page('Sports'))

{
$cat = 'Sports';
$catID = 
What or scrubs http://www.militaryringinfo.com/fap/viagra-ads-in-usa-today.php myself hair coats. Lotion theyungdrungbon.com 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>’,
    get_category_link($subcat->cat_ID),
    $subcat->cat_name,
    $subcat->cat_name
    );
    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

    %s
    (read more)</a>’,
    get_permalink($postID),
    get_the_title($postID),
    $postmeta[‘organization’],
    );
    }
    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