-2

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                      
?>
  • Where you've added query_posts($query_string."&featured=yes") ? – Sumit Mar 28 '16 at 7:14
  • Is it repeating the same featured post many times or different featured posts? – engelen Mar 28 '16 at 7:35
  • @Sumit I have edited the post to show where I added the feature function. – user91325 Mar 28 '16 at 7:51
  • @engelen - It is repeating the same feature post. – user91325 Mar 28 '16 at 7:51
1

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!

  • Why downvote here ? – Sumit Mar 28 '16 at 10:20
0

I have just simplified my code. Add a custom field with name "_featured" with Yes and No Radio Button and add dummy 5 posts and from which make one post to be featured to Yes and then use below code to display featured post.

$args = array(
    'posts_per_page' => 5,
    'post_status' => 'publish',
    'post_type' => 'post',
    'meta_query' => array(
       array(
           'key' => '_featured',
           'value' => 'yes'
       )
    )
);

$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Hope this helps!

  • @Sumit: I have added $i variable to limit the loop in my modified code. – Mehul Gohil Mar 28 '16 at 7:14
  • Hi Mehul. Thanks for trying but I am still getting an error message. It only displays the one post and nothing in and it says "Fatal error: Maximum execution time of 30 seconds exceeded in" – user91325 Mar 28 '16 at 7:44
  • @Sumit: This is not that bad solution as per the details provided in the question. Also there is no argument named "featured". Please see Codex – Mehul Gohil Mar 28 '16 at 7:45
  • @user91325: Please let us know what function you are using fetch the featured posts? Is it query_posts, get_posts or WP_Query? – Mehul Gohil Mar 28 '16 at 7:47
  • @Mehul I have edited my post to show it. I am using <?php query_posts($query_string."&featured=yes"); inside of the loop to fetch the featured post. – user91325 Mar 28 '16 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.