• So I’ve been asked to help out with a wordpress site by adding a section for recent blog posts on the homepage. I’ve put in basically a carbon copy of the loop example in the codex, but instead of displaying recent posts, it displays a link to the homepage as if it were a blog post. There are definitely posts that it should be displaying, and home is definitely a page not a post. I wonder if the people who designed the site did some wired setting or something, but I’m just really confused. Any help?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Can you copy and paste the code here?

    Also, if you’re running multiple loops on the same page, you should reset the first loop, to allow the second loop to run.

    your first loop should end as such:

    <?php endwhile; // end of the loop.
    wp_reset_query(); // Reset Query
    ?>

    Thread Starter nblaisdell

    (@nblaisdell)

    I mean it’s literally just from the codex. I was having the same problem when I tried before with hand coded stuff so I just cut and pasted their code to try and fix it thinking that was the problem but it didn’t work.

    <!-- Start the Loop. -->
     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
     <!-- Test if the current post is in category 3. -->
     <!-- If it is, the div box is given the CSS class "post-cat-three". -->
     <!-- Otherwise, the div box is given the CSS class "post". -->
    
     <?php if ( in_category('3') ) { ?>
               <div class="post-cat-three">
     <?php } else { ?>
               <div class="post">
     <?php } ?>
    
     <!-- Display the Title as a link to the Post's permalink. -->
    
     <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
     <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
    
     <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
    
     <!-- Display the Post's content in a div box. -->
    
     <div class="entry">
       <?php the_content(); ?>
     </div>
    
     <!-- Display a comma separated list of the Post's Categories. -->
    
     <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
     </div> <!-- closes the first div box -->
    
     <!-- Stop The Loop (but note the "else:" - see next line). -->
    
     <?php endwhile; else: ?>
    
     <!-- The very first "if" tested to see if there were any Posts to -->
     <!-- display.  This "else" part tells what do if there weren't any. -->
     <p>Sorry, no posts matched your criteria.</p>
    
     <!-- REALLY stop The Loop. -->
     <?php endif; ?>

    This is the site if that helps. The problem is at the bottom under “The Leaflet” – that’s where the blog excepts will eventually go:
    https://theleaves.org/

    I need a little more info and I think I can help…

    Are you just displaying recent posts or is there a reason to have a conditional statement?

    For example, under the leaflet, you’re just going to show the 5 (or however many you want) most recent posts, regardless of category right?

    If so, then do this:

    <?php
    // The Loop
    while ( have_posts() ) : the_post(); ?>
    
    <div class="post-cat-three">
    
    <h2>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></h2>
    <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
    
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <h2 class="entry-title">" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></h2>
    
    <div class="entry-content">
    	<?php the_excerpt( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyten' ) ); ?>
    	 <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
    </div><!-- .entry-content -->
    
    </div><!-- #post-## -->
    <?php endwhile; // end of the loop. ?>
    
    Thread Starter nblaisdell

    (@nblaisdell)

    Yeah it’s just going to be an excerpt from the most recent post, regardless of category. Unfortunately that code just displays a link to the home page as well.

    Can you show the code for the rest of the page?

    I edited the loop. I forgot to include the arguments and reset the query. However, if there is a query running on the page prior to this one, it needs to have the reset_query after the enwhile, just as below.

    <?php
    $args = array( 'posts_per_page' => 1, 'orderby' => DESC);
    // The Query
    query_posts( $args );
    // The Loop
    while ( have_posts() ) : the_post(); ?>
    
    <div class="post-cat-three">
    
    <h2>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></h2>
    <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
    
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <h2 class="entry-title">" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></h2>
    
    <div class="entry-content">
    	<?php the_excerpt( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyten' ) ); ?>
    	 <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
    </div><!-- .entry-content -->
    
    </div><!-- #post-## -->
    <?php endwhile; // end of the loop.
    	wp_reset_query();  // Reset Query
    	?>
    

    Try modifying your code above so that you don’t use query_posts. Try placing the following 3 lines in place of the query_posts call:

    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query( $args );

    Then keep the loop as you have it

    Then before reset query call (keep that call in there):

    $wp_query = null;
    $wp_query = $temp;

    Any luck with that?

    Marvin,

    is this what the full code should look like for nblaisdell….

    <?php
    $args = array( 'posts_per_page' => 1, 'orderby' => DESC);
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query( $args );
    // The Loop
    while ( have_posts() ) : the_post(); ?>
    
    <div class="post-cat-three">
    
    <h2>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></h2>
    <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
    
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <h2 class="entry-title">" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></h2>
    
    <div class="entry-content">
    	<?php the_excerpt( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyten' ) ); ?>
    	 <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
    </div><!-- .entry-content -->
    
    </div><!-- #post-## -->
    <?php endwhile; // end of the loop.
    $wp_query = null;
    $wp_query = $temp;
    	wp_reset_query();  // Reset Query
    	?>
    

    Looks good to me. This is a sound approach of you are trying to perform multiple loops in a page. query_posts does, I believe, produce unexpected behavior if there’s more than one loop in play.

    Hope this works for you.

    John

    Tom — sorry for being lazy! Thanks for incorporating. I had kind of misread the thread and thought that code was by the thread author, so hope my modification didn’t sound rude.

    No worries Marvin.
    I’ve had mixed result using query_posts, new WP_query, and get_posts().

    I’m always open to better ways of writing clean code ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Loop not displaying posts, instead has a link to the homepage’ is closed to new replies.