wp_get_archives doesn't work as array (6 posts)

  1. Bijan641
    Posted 4 years ago #

    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.

  2. Bijan641
    Posted 4 years ago #

    Wow this got pushed to page 5 in a matter of hours. Sorry, but I need to bump this!

  3. keesiemeijer
    Posted 4 years ago #

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

  4. Bijan641
    Posted 4 years ago #

    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.

  5. Curtiss Grymala
    Posted 4 years ago #

    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.

  6. Bijan641
    Posted 4 years ago #

    Thank you! That is very helpful.

Topic Closed

This topic has been closed to new replies.

About this Topic