How to Display Featured Posts in WordPress

How to Display Featured Posts in WordPress thumbnail
Add a special loop to look for featured posts.

While WordPress does not do anything special for featured posts out of the box, you can add the functionality to any WordPress theme. Add a category named "Featured" and then create a loop - the code structure WordPress uses to display posts - that displays all posts in the "Featured" category. All you need is a query to find those featured posts and then the loop itself, followed by a reset so it will not interfere with other loops on the page. More experienced coders can then modify how WordPress displays the featured posts using JavaScript.

Instructions

    • 1

      Open the "index.php" file of your current WordPress theme in a code editor. WordPress calls its templates "themes," and you can find their files in the following location:

      /site-name/wp-content/themes/theme-name/

      Change "site-name" to whatever you named your main WordPress folder and change "theme-name" to the name of the theme you want to edit.

    • 2

      Copy and paste the contents of "index.php" into a new file and save it as "home.php". When WordPress finds a "home.php" file in a theme folder, it uses that file to display the first page of your blog if the blog is your home page. This is in contrast to the "front-page.php" WordPress uses to display contents of a static home page.

    • 3

      Find the WordPress loop in the code of "home.php". It starts like this:

      <?php if(have_posts()) : ?>

      Sometimes the loop is all on one line, but sometimes you will see it broken up. Add a few blank spaces above the loop and add this code:

      <?php query_posts(); ?>

      <?php if(have_posts()) : while(have_posts()) : the_post() : ?>

      <?php endwhile; endif; ?>

      <?php wp_query_reset(); ?>

      The code above sets a query, begins a new loop, ends the loop and then resets the query. You must reset the query to avoid effecting the second loop.

    • 4

      Write your query between the parantheses after "query_posts". You need to query featured posts, so use the following code:

      <?php query_posts(array('category_name' => 'Featured')); ?>

      Save the file and upload it to your server.

    • 5

      Log into the WordPress dashboard and add the category name "Featured" to all featured posts. Update them and then visit your website to see the results. Featured posts will display first on the first page of your blog, and then all posts will display beneath them.

Tips & Warnings

  • Set a limit to how many featured posts will display on the first page of your blog. Change your query to look like this:

  • <?php query_posts(array('category_name' => 'Featured', 'posts_per_page=5')); ?>

  • Replace "5" with any number you want.

Related Searches:

References

  • Photo Credit Jupiterimages/Photos.com/Getty Images

Comments

You May Also Like

Related Ads

Featured