WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Bug? (Multiple Loops in Page gives wrong info) (4 posts)

  1. Dazzer
    Member
    Posted 6 years ago #

    If you read my previous post you'll remember that I was working on a static front page, displaying some posts.

    This is the first part of the code:

    <?php
       if ( have_posts() ) :
          the_post();
          ?><h2><?php the_title(); ?></h2><?php
          the_content();
       else:
          ?>Someone Broke the About Page<?php
       endif;
    ?>

    That works perfectly fine! Now I do this

    <?php
       $posts = get_posts( 'numberposts=2' );
       foreach ($posts as $post) :
          setup_postdata($post);
          ?>
          <h3>
          <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a>
          </h3>
          <h4>
              Posted <?php the_date() ?>
              - Comments (<?php comments_number(); ?>)
          </h4>
    
              <?php the_excerpt(); ?>
    
       <?php
          endforeach;
       ?>

    And while the dates, comment count, excerpt all work perfectly, the_ID never echos, the_title always gives me the title of the Page, not the post, and same for the permalink.

    Right now I have circumvented this issue by accessing properties of the $post object directly (guid, post_title, and ID) but I believe if I do it like so, plugins won't be able to hook onto it because I'm not calling the hook, correct?

    So what am I doing wrong? Or is this a bug? Is there a better workaround?

    Many Thanks
    Dazzer

  2. Dazzer
    Member
    Posted 6 years ago #

    Resolved: instead of using get_posts, which seems to get the posts for the Page as well, I used query_posts('posts_per_page=1'); and Loop through accordingly.

    Hope this helps anyone on the same track. Urgh...

  3. klagreca
    Member
    Posted 6 years ago #

    Not sure if this is related, but I too want to display the most recent post on a Page. However, query includes Pages as well. Here's what I have in my custom page template:

    <h2 class="entry-title">Most Recent Posting</h2>
     <?php
    query_posts('posts_per_page=1');
       foreach ($posts as $post) :
          setup_postdata($post); ?>
     <div>
     <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
     <?php the_date(); ?>
     <?php the_excerpt(); ?>
     </div>
       <?php
          endforeach;
       ?>

    Any ideas what's up here? I also tried a get_posts query as well. The results are the same.

    Thanks!
    Kris

  4. klagreca
    Member
    Posted 6 years ago #

    Figured it out. If anyone is interested, I "reset the loop" by using the rewind_posts() call. Here's what I have now:

    <?php get_header() ?>
    
    	<div id="container">
    	<?php get_sidebar() ?>
    		<div id="content">
    
    <?php the_post() ?>
    			<div id="post-<?php the_ID(); ?>" class="<?php sandbox_post_class() ?>">
    				<h2 class="entry-title"><?php the_title(); ?></h2>
    				<div class="entry-content">
    <?php the_content() ?>
    <br/ >
    <h2 class="entry-title">Most Recent Posting</h2>
      <?php rewind_posts(); ?>
     <?php query_posts('showposts=1');?>
        <?php while (have_posts()) : the_post(); ?>
     <div>
     <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
     <?php the_date(); ?>
     <?php the_excerpt(); ?>
     </div>
      <?php endwhile;?>
      				</div>
    			</div><!-- .post -->
    		</div><!-- #content -->
    	</div><!-- #container -->
    <?php get_footer() ?>

    Have fun!
    Kris

Topic Closed

This topic has been closed to new replies.

About this Topic