WordPress.org

Ready to get started?Download WordPress

Forums

Loop stuck on get_categories and then foreach (13 posts)

  1. Jack
    Member
    Posted 3 years ago #

    I'm using this code to style top level categories that have posts differently from the ones that don't have posts. But the loop gets stuck now that I have more categories. It worked well with 200 categories, but now I have about 3000 categories and it get stuck on the first one.

    Has this to do with php limits or WordPress limits?

    $allcats = get_categories('hide_empty=0&exclude=1&orderby=slug&order=ASC');
    foreach($allcats as $thecat) {
    	$test=get_posts('category='.$thecat->cat_ID);
    	if ($thecat->parent < 1) {
    	if ($test) {
    	echo "<li><a href=\"" . get_category_link($thecat->cat_ID) . "\"><span>". get_cat_name($thecat->cat_ID) ."</span></a></li>";
    	} else {
    	echo "<li class=\"maincatinactief\"><span>". get_cat_name($thecat->cat_ID) ."</span></li>";
    	}
    	}
    }
  2. wprock
    Member
    Posted 3 years ago #

    user "number" in the function

  3. Jack
    Member
    Posted 3 years ago #

    What function?

  4. wprock
    Member
    Posted 3 years ago #

    use like this get_categories('hide_empty=0&exclude=1&orderby=slug&order=ASC&number=10');

  5. Jack
    Member
    Posted 3 years ago #

    Thanks. I tried that with number=20 or number=20000 but no results. When I set hide_empty to 1 the code above works, when set to 0 it doesn't, even with number set to 20000.

  6. keesiemeijer
    moderator
    Posted 3 years ago #

    couldn't you do something like this to test if top level categories have posts:

    <?php
    $allcats = get_categories('hide_empty=0&exclude=1&orderby=slug&order=ASC');
    
    foreach($allcats as $thecat) {
    	if ($thecat->parent == 0) {
    	  if($thecat->count > 0) {
    	    // top level category with posts
    	  } else {
    	    // top level category without posts
    	  }
    	}
    }
    
    ?>
  7. Jack
    Member
    Posted 3 years ago #

    It looks like the code of keesiemeijer is working. No idea why that other code isn't. Keesie, is this code 'bullet proof'? ;) I mean, I have parent > first child > second child categories, and only the second child categories actually have posts, the rest is pure for navigation purposes.

  8. keesiemeijer
    moderator
    Posted 3 years ago #

    I think so, you can also do an else for child categories:

    if ($thecat->parent == 0) {
    	  if($thecat->count > 0) { // post count
    	    // top level category with posts
    	  } else {
    	    // top level category without posts
    	  }
    } else {
    // do stuff for child categories here
              if($thecat->count > 0) { // post count
    	    // child category with posts
    	  } else {
    	    // child category without posts
    	  }
    }
  9. Jack
    Member
    Posted 3 years ago #

    Looks good. The only thing is it will only work when using the checkboxes for all levels: the top level parent, the first child category and the category on the second and deepest level. That's no problem, but now my breadcrumbs aren't working anymore :( Perhaps looking for another breadcrumb plugin.

  10. keesiemeijer
    moderator
    Posted 3 years ago #

    Did they work before my code? I think (not tested) in your code the conditionals should be this way around:

    if ($test) { // category has posts
    if ($thecat->parent < 1) { // this is a top level category
  11. Jack
    Member
    Posted 3 years ago #

    Which code is faster? In my case that's rather important. so if your code is faster, I'm happy to look for another plugin.

  12. keesiemeijer
    moderator
    Posted 3 years ago #

    Well, my code is faster because it doesn't need to query all posts belonging to a category (they are part of the the original query from get_categories()).

  13. Jack
    Member
    Posted 3 years ago #

    Ah, ok :) That sounds good! Do you by any chance also know how I could get the top level category to display in my searchresults? the_category() shows all three of them, but I only need the top level cat.

Topic Closed

This topic has been closed to new replies.

About this Topic