Support » Fixing WordPress » query_posts outside the loop?

  • Resolved PeacefulKarl

    (@peacefulkarl)


    I have placed a query_posts element outside the loop in the header so wordpress is calling that tag instead of the loop tag. I am using it to create a list of recent posts. Is there another way of doing this?

    Thanks Karl

Viewing 7 replies - 1 through 7 (of 7 total)
  • Chris_K

    (@handysolo)

    Should we assume this is a dupe of http://wordpress.org/support/topic/111724?replies=1 or are there more details that you haven’t provided yet?

    PeacefulKarl

    (@peacefulkarl)

    Handy, I’ve been searching everywhere for an answer for this and just need a little ‘nudge’. I guess you might call it a dupe but I prefer to think of it as a call for help…

    I have tried
    <?get_posts(‘showposts=6’;?>
    and it returns only one post…

    I have tried
    <?php wp_get_archives(‘type=postbypost&limit=6’); ?>
    and it returns 6 posts but without dates preceding the title…

    I have a link below that points to the permalink and
    <?php the_time(‘m.j’) ?></span> <?php the_title() ?>
    to get the date and title…

    what am I missing here?

    sorry to be that guy that wont quit posting, I swore I would never be him… But tonight I have no answers…

    alakhnor

    (@alakhnor)

    The correct syntax is :

    $myposts= get_posts('numberposts=6');
    foreach ($mypost as $post) :
       [bla...bla]
    endforeach;

    PeacefulKarl

    (@peacefulkarl)

    I tried it but got an error message. I am trying to put it in the side bar… does that matter?

    PeacefulKarl

    (@peacefulkarl)

    It looks right in the navigation bar at the top right now but is reeking havoc on every page but the index because the query_posts element is outside the loop…

    OK… I got it running sort of… my code looks like this…

    <?php$myposts= get_posts(‘numberposts=6’);?>
    <? foreach ($myposts as $post) : if (have_posts()) :while (have_posts()) : the_post(); ?>
    [ bla… bla…}
    <?php endwhile; endif: ?>
    <?php endforeach;?>

    it has solved part of the problem but now the list only shows the latest post over and over instead of the last six different posts… what did I miss in there?

    WOOOHOO It took some doing but I got it….
    I am no php wizard but your code works…

    thanks
    alakhnor

    Karl

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘query_posts outside the loop?’ is closed to new replies.