Support » How-To and Troubleshooting » wp_get_archives doesn't work as array

wp_get_archives doesn't work as array

  • wp_get_archives works unless I try to output it to an array. If I do that I get this error:

    Warning: Invalid argument supplied for foreach() in /home/dotswoof/public_html/beta/wp-content/themes/swoof/archive.php on line 92

    Here is the link:

    Here is the code:

    $args = array(
                        'type'            => 'postbypost',
                        'format'          => 'link',
                        'show_post_count' => false,
                        'echo'            => 0 );
                        $archive = wp_get_archives($args);
                        foreach ($archive as $post)
                            echo $post->post_name;

    I don’t know why this isn’t working and I’m on a deadline. Any help would be greatly appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Wow this got pushed to page 5 in a matter of hours. Sorry, but I need to bump this!

    Moderator keesiemeijer


    wp_get_archives does not return an array so it can’t be used in a foreach loop

    This is would it says on the codex page:

    (boolean) Display the output or return it.
    1 (True) – Default
    0 (False)

    I assume return it must mean an array. What else could it mean? And if this is not the case, is there any other way to complete control the format of the output?

    For example, I can’t seem to find a way to post the date and the name in list format.

    Unfortunately, you’re assuming incorrectly. wp_get_archives() can either echo the HTML code for the archive list, or it can return that HTML code as a variable. It does not return an array. It builds the HTML and either displays it or returns it.

    If you want to tweak the output, you’re either going to have to do some sort of search and replace on the returned HTML, or you’re going to have to build your own function to retrieve the archive list as an array.

    Thank you! That is very helpful.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_get_archives doesn't work as array’ is closed to new replies.