WordPress.org

Forums

get_posts inside my loop is breaking my loop (2 posts)

  1. I like screenshots
    Member
    Posted 1 year ago #

    This piece of code is screwing up my loop on my single-CCCC.php custom post type template

    <?php $posts = $posts = get_posts( array('include' => implode( ',', $_SESSION['pid'] ), 'post_type' => array('AAAA','BBBB'),'orderby' => 'rand','numberposts' => '5',) ); foreach($posts as $post) { ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>, <?php } ?>

    My loop is this...
    <?php if( have_posts() ) : ?><?php while( have_posts() ) : the_post(); ?>
    then this
    <?php the_title(); ?> which works fine
    then
    <?php $posts = $posts = get_posts( array('include' => implode( ',', $_SESSION['pid'] ), 'post_type' => array('AAAA','BBBB'),'orderby' => 'rand','numberposts' => '5',) ); foreach($posts as $post) { ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>, <?php } ?>
    then
    <?php the_title(); ?> which doesn't work
    then this
    <?php endwhile; else: endif; ?>

  2. I like screenshots
    Member
    Posted 1 year ago #

    Fixed it, I just closed the loop before the get_post and reopened the loop after the get_post

Topic Closed

This topic has been closed to new replies.

About this Topic