CodeWise Blog

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 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 = 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'; 

2 Responses »

  1. 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. Jarret, thanks for that. I adjusted the HTML so it shows up better.

Leave a Response

Subscribe without commenting

Back to Top