Forums

[resolved] Random post + Recent posts + Last post = Loop problem? (3 posts)

  1. Alkorr
    Member
    Posted 1 year ago #

    Hi, I want to show a random post in my header, and a list my recent posts + my last post on my index page. The problem is, they don't work well together and when I click on a link, I get the wrong post...

    Here is my code for the random post:

    <?php
    query_posts(array('orderby' => 'rand', 'posts_per_page' => 1));
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    <?php the_excerpt(); ?>
    <?php endwhile;
    endif; ?>

    Here is my code for the (3) recent posts:

    <?php query_posts('posts_per_page=3&offset=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>

    And here my loop on the main page:

    <?php query_posts('posts_per_page=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>

    I tried everything I could, checked everything but I guess I must have missed something because it doesn't work. Thank you very much for your help, I'm stucked!

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    try to add
    <?php wp_reset_query(); ?>
    before the codes for the second and third loop.

    http://codex.wordpress.org/Function_Reference/wp_reset_query

  3. Alkorr
    Member
    Posted 1 year ago #

    Thank you so much alchymyth! Now it works perfectly! :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.