• Hi all,

    I pasted the example of get_posts from the support docs into my site and I am getting the following error:

    “invalid argument supplied for foreach()”

    I am using the latest version of WP and here is the code:


    <ul>
    <?php
    $posts = get_posts('numberposts=5&offset=1&category=1');
    foreach($posts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> --- <?php the_excerpt(); ?></li>
    <?php endforeach; ?>
    </ul>

Viewing 1 replies (of 1 total)
  • hussv1

    (@hussv1)

    I got that error when my query doest return any results.
    You need to check for that first, like this:
    <?php
    $posts = get_posts(‘numberposts=5&offset=1&category=1’);
    if ($posts!=”) {
    foreach($posts as $post) :
    ?>

Viewing 1 replies (of 1 total)
  • The topic ‘Get_posts example from DOCS not working.’ is closed to new replies.