WordPress.org

Ready to get started?Download WordPress

Forums

Category Display: Invalid foreach() (11 posts)

  1. fredb86
    Member
    Posted 10 years ago #

    I've been seeing this warning pop up for many people using WP since WP 1.0 and it just appeared for me again after I upgraded from WP 1.02 -> 1.2. Can we please get an official fix for this problem?
    Yes, I have 10 categories but none are displayed. Yes I can easily create new categories but those don't show up either.
    Summary of problem:
    This line of code: <?php wp_list_cats(); ?>
    causes this warning to appear:
    Warning: Invalid argument supplied for foreach() in /home/myblog/wp-includes/template-functions-category.php on line 304'
    My configuration: MySQL 4.x, PHP 4.36, FreeBSD

  2. unoamigo
    Member
    Posted 10 years ago #

    If you can, post your template, or e-mail it to me: unoamigo@frankentosh.com
    If I remember right it throws that error because you are calling wp_list_cats from within the loop for displaying the blog contents.
    Most likely this is a template problem.

  3. fredb86
    Member
    Posted 10 years ago #

    That is not the case here. I did a fresh install of WP 1.2 and changed the site url in the database, and I still get this message when using the default index.php and css file from WP 1.2.

  4. unoamigo
    Member
    Posted 10 years ago #

    <?php list_cats(0, 'All', 'name', '', '', false); ?>
    That work for you?

  5. fredb86
    Member
    Posted 10 years ago #

    It does not work. However,
    if I use this code:
    <?php list_cats(0, 'All', 'name', '', '', '', '', '', false); ?>
    the categories are displayed BUT I STILL GET THE ERROR MESSAGE.

  6. Mark (podz)
    Support Maven
    Posted 10 years ago #

    I have this error in 2 blogs
    1.2mingus
    1.2delta
    Both blogs worked fine last night, but now they are b0rked...
    The loops are fine, and I have not altered the default cat display php statement.

  7. Mark (podz)
    Support Maven
    Posted 10 years ago #

    Got mine.
    Some php tracking code which I had must be interfering with a wp variable. I deleted the tracking code, and the problem went away.
    Still need a script though :(

  8. fredb86
    Member
    Posted 10 years ago #

    Ok. I'm pretty sure I figured out why it's happening. Here's my setup. If someone could try this an verify, I'd truly appreciate it.
    1) I have ten categories
    2) I have 20 articles but ALL of them have "Post Status" set to "Private"
    That causes the error message in index.php when categories are listed.
    3) If I set the "Post Status" of just one of the articles to "Publish", the error DOES NOT appear. The article is displayed and just the category that the article is in is listed.
    So, this appears to be a bug in WordPress where, if all articles have "Post Status" set to "Private", an attempt to list categories causes that error msg to appear.

  9. fredb86
    Member
    Posted 10 years ago #

  10. Anonymous
    Unregistered
    Posted 10 years ago #

    The problem is that the query to pull the categories returns nothing, so the $cat_counts array is empty. Changing lines 304-308 of wp-includes/template-functions-category.php from:

    foreach ($cat_counts as $cat_count) {
    if (1 != intval($hide_empty) || $cat_count > 0) {
    $category_posts["$cat_count->cat_ID"] = $cat_count->cat_count;
    }
    }

    to this:

    if (count($cat_counts)) {
    foreach ($cat_counts as $cat_count) {
    if (1 != intval($hide_empty) || $cat_count > 0) {
    $category_posts["$cat_count->cat_ID"] = $cat_count->cat_count;
    }
    }
    }

    should fix this error.

  11. Ryan Boren
    WordPress Dev
    Posted 10 years ago #

    Thanks. Fixed in CVS.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.