I am trying to create a part on my wordpress theme that will only display one featured post. I have installed this plugin – https://wordpress.org/plugins/featured-post/ .
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?
<?php if ( have_posts() ) { while ( have_posts() ) { the_post(); query_posts($query_string . "&featured=yes"); the_author(); if ( has_post_thumbnail() ) { the_post_thumbnail('thumbnail'); } ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php the_excerpt();?> <a href="<?php the_permalink(); ?>"> Read More.... </a> <?php } // end while } // end if ?>
Answer
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.
Example:
global $query_string; //Get current query arguments
query_posts($query_string."&featured=yes");
if (have_posts()) {
while (have_posts()) {
the_post(); ?>
<? php the_author(); ?>
<? php
if (has_post_thumbnail()) {
the_post_thumbnail('thumbnail');
} ?>
<a href = "<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
<a href = "<?php the_permalink(); ?>"> Read More.... </a>
<?php
} // end while
} // end if
NOTE:
query_posts()
is not recommended please ask plugin support to
provide alternative way to use their plugin!
Attribution
Source : Link , Question Author : user91325 , Answer Author : Sumit