• Resolved Anna

    (@ohthenoes)


    I was hoping to do all of this within the category.php template, with help from the functions.php if needed.

    If my setup is:

    Parent 1
    --Child 1
    --Child 2
    Parent 2
    --Child 3
    --Child 4
    Parent 3
    --Child 5
    --Child 6
    
    ...and so on

    I would like to be able to do this:

    If I was viewing Parent 1 (or 2/3), it would show me Child 1 and Child 2. However, I don’t want to use wp_list_categories because for each Child, I would like to show a thumbnail (from a plugin function), the Child’s name, and the category description.

    If I was viewing Child 1 (or 2/3/4/5/6), it would show me the latest 2 posts using the standard Loop.

    I would like avoiding having to do this using hard-coded contitionals with the specific names of the categories, in case new ones will be added in the future.

    Any assistance will be appreciated. Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Anna

    (@ohthenoes)

    Solved, thanks to nofearinc on the wp.org irc chat! ??

    https://gist.github.com/mpeshev/cb1cd3c56d06017b3d87

    tomhermans

    (@tomhermans)

    Handiest is if you have an array of childcats, so you can use in_category() to display stuff or not.

    I wrote this function which gives you that array of childcategories, based on the name of the parent-category.

    // Get List of Child Categories
    function get_child_cats( $catname ) {
    $parentcat = get_cat_ID( ‘$catname’ );
    $subcat = get_categories( array(‘child_of’ => $parentcat ) );
    $cat_array = array();
    //array_push($cat_array, $parentcat); // add the parent cat to the array
    foreach ($subcat as $sc) {
    array_push($cat_array, $sc->cat_ID);
    }
    return $cat_array;
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How do I show child categories if in a parent category, and posts if in a child?’ is closed to new replies.