WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Multiple Loops and getting categories (4 posts)

  1. brockangelo
    Member
    Posted 5 years ago #

    I'm using multiple loops on a template page. In each instance, I'm using

    new WP_Query...

    But towards the bottom, I've been trying to list the children of the current category. Then, iff there are any children, I'm planning to list the pages categorized as such.

    When I var_dump the array created by the get_categories function, it will display the values of the array in Firefox, but IE shows array(0) because of the whole multiple loop issue.

    What I have not figured out is how to create a new WP_Query to list category children. If I could get that, I could show the pages associated with it, but WP_Query doesn't seem to support calling categories.

    It looks like using "WP" might, or almost certainly "wpdb" but I haven't gotten this working. Anyone have any experience with this?

  2. MichaelH
    Member
    Posted 5 years ago #

    List category children would be done with the template tag, wp_list_categories(), child_of= argument.

  3. brockangelo
    Member
    Posted 5 years ago #

    Thanks Michael - I tried that one, but with multiple loops the standard wp functions don't return reliable results. In the case of wp_list_categories, I get "No Categories".

    I dug more into the $wpdb function, since I've never really done much with this and was able to figure this one out. For those who need this as well, here is my solution. I have put my category ID (the category number) into a variable called "$deptID" since I've tied my departments to matching categories:

    $childCatID = $wpdb->get_col("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=$deptID");
    	if ($childCatID){
    	foreach ($childCatID as $kid)
    	{
    	$childCatName = $wpdb->get_row("SELECT name, term_id FROM $wpdb->terms WHERE term_id=$kid");
    	echo $childCatName->term_id;
    	echo $childCatName->name;
    	}
    
    	?>

    The code above will check the category to see if there are any sub-categories, and will display the results. I've been using it in a collapsible div to show and hide pages that are in the subcategories. The term_id has been useful for creating unique id's for the collapsible divs, since you'll have multiple categories.

    I hope this helps someone. It's another workaround for the CMS gap. It has been nagging me for weeks. :-)

  4. zigx
    Member
    Posted 5 years ago #

    brockangelo

    Thanks a lot for taking the time to post your solution. This has helped me quite a bit.

Topic Closed

This topic has been closed to new replies.

About this Topic