• 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>";
    	}
    	}
    }
Viewing 12 replies - 1 through 12 (of 12 total)
  • user “number” in the function

    Thread Starter Jack

    (@moxie)

    What function?

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

    Thread Starter Jack

    (@moxie)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    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
    	  }
    	}
    }
    
    ?>

    Thread Starter Jack

    (@moxie)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    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
    	  }
    }

    Thread Starter Jack

    (@moxie)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    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

    Thread Starter Jack

    (@moxie)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    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()).

    Thread Starter Jack

    (@moxie)

    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.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Loop stuck on get_categories and then foreach’ is closed to new replies.