Forums

Get_posts example from DOCS not working. (2 posts)

  1. evergreen
    Member
    Posted 5 years ago #

    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>

  2. hussv1
    Member
    Posted 5 years ago #

    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) :
    ?>

  3. "><?php the_title(); ?> --- <?php the_excerpt(); ?>
  4. <?php endforeach; ?>
    <?}?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags