The code below is based on a couple of forum posts from a couple of places. To a certain extent, it works. The issue I am having is that it only shows "Activities", no matter what parent category I'm in. I tried replacing "activities" with a number of very unsuccessful alternatives (conditionals, get functions, and others); nothing worked.

<?php $cat = 'Activities'; /* This is the problem area.
   Trying to get $cat to equal the name of the selected parent categories; the rest of
   the code seems to be working, at least for the Activities category. */

$catID = get_cat_ID($cat);
echo '<h2>' . $cat . '<h2>';
$subcats = get_categories('child_of=' . $catID);
foreach($subcats as $subcat)
    echo '<h4>' . $subcat->cat_name . ' </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 '</a>';
        echo '</li>';
    echo '</ul>';

If you are in a category context this should work and and you'll get the correct catID to work with.

function getCurrentCatID(){
    global $wp_query;
        $cat_ID = get_query_var('cat');
    return $cat_ID;
$catID = getCurrentCatID();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.