WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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>';
share|improve this question
up vote 0 down vote accepted

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();
share|improve this answer
thanks for your reply... got it all working now... – Michael Mar 5 '12 at 7:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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