Forums

Problems using two loops (5 posts)

  1. fredkelly
    Member
    Posted 3 years ago #

    Hi There,

    I have created my own theme and everything is running okay.

    I want to have a list of the top 5 latest news items visible on every page of the site, this includes post & page.php pages.

    This is the basic code of my list:

    <ul>
                    <?php while (have_posts()): the_post(); ?>
                		<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
                    <?php endwhile; ?>
                    <?php rewind_posts(); ?>
    </ul>

    This is run before the main wordpress loop.

    I am finding that when I visit a page (i.e. using the page.php file) this list shows just the title of the current page, not the latest blog posts.

    I think this is because my code is reading from whatever is in the post array which is changing for each page. I am not, however, sure how I can solve this isse so that the list is the same on every page independent of any other loops.

    Any help on this one would be much appreciated!
    - Fred

  2. jonimueller
    Member
    Posted 3 years ago #

    I don't see anywhere in your code where you tell WP to pull in the latest five posts. And then it is always going to put the latest post on the front page of the blog anyway. There is a workaround for that too. Have you read the Codex pertaining to The WordPress Loop? Start there.
    http://codex.wordpress.org/The_Loop
    http://codex.wordpress.org/The_Loop_in_Action

  3. fredkelly
    Member
    Posted 3 years ago #

    Hi There,

    Sorry, I wasn't very clear, I have not got to adding the code to show the 5 latest posts as I am still trying to solve this problem.

    Basically, I cannot make the loops run independantly of each other, when the code is running on the news page it shows the news items, when it is run on a page.php page it shows the title of that page.

    I want it to constantly show 5 newest blog posts independant of which page the code is included (its in my header.php file).

    Thanks.

  4. jonimueller
    Member
    Posted 3 years ago #

    If you want it to show on a Page, then put the loop code in your page.php template.

  5. jbbrwcky
    Member
    Posted 3 years ago #

    You may want to consider storing the loop in a variable, then going back to the variable and performing whatever alterations you need on it. Like so:

    $my_query = new WP_Query('showposts=5'); ?>
    while ($my_query->have_posts()) : $my_query->the_post();
      // your post coding here
    endwhile;

    If you wanted to use the loop again, alter it:

    $my_query = new WP_Query('cat=4');

    And so on...

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags