How to create a loop that will display one post and stop?

I am trying to create a part on my wordpress theme that will only display one featured post. I have installed this plugin – .

I know I am supposed to integrate <?php query_posts($query_string."&featured=yes"); somewhere into the loop to output the featured post. I tried modifying the code from my existing loop that displays regular posts but the results show the featured post repeated many times.

This is the loop I am trying to use that displays the featured post but repeats it a ton of times. How can I get it to only display once?

if ( have_posts() ) {
    while ( have_posts() ) {

        query_posts($query_string . "&featured=yes");


        if ( has_post_thumbnail() ) {

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_excerpt();?>  
<a href="<?php the_permalink(); ?>"> Read More.... </a>   

    } // end while
} // end if                      


query_posts() can alter the WP query even when it is executed and it should be placed at least before you start the loop i.e. have_posts(). Currently you are placing it after setting up post data i.e. the_post(). Which is resulting in unexpected behavior.


global $query_string; //Get current query arguments

if (have_posts()) {
    while (have_posts()) {
        the_post(); ?>    

        <? php the_author(); ?>

        <? php
        if (has_post_thumbnail()) {
        } ?>

        <a href = "<?php the_permalink(); ?>"><?php the_title(); ?></a> 
        <?php the_excerpt(); ?> 
        <a href = "<?php the_permalink(); ?>"> Read More.... </a>   

    } // end while
} // end if

NOTE: query_posts() is not recommended please ask plugin support to
provide alternative way to use their plugin!

Source : Link , Question Author : user91325 , Answer Author : Sumit

Leave a Comment