Top Ad unit 728 × 90

Display the most Recent Post from a Specific Category


Did you see sites with a magazine style theme who are displaying posts from a specific category. Sometimes only the most recent post. Well you can do this too easily.
By default, your main WordPress blog page displays your most recent posts in descending date order. But if you're using categories on your site and your readers will want to see what's new in each category, you might want your blog page to look different.
In this tutorial I'll show you how to do just that. I'll demonstrate how to:
  • identify all of the categories on your blog
  • display the most recent post for each one, with a featured image if the post has one
  • make sure that posts in more than one category don't get duplicated
  • add some styling to make it look good
  • To follow this tutorial, you'll need:
    • A development installation of WordPress.
    • Some posts and categories set up. I've used a sample of the data from the WordPress theme unit test data.
    • A theme. I'm going to create a child theme of the Twenty Fourteen theme.
    • A code editor.
    The first step is to get the theme set up. I'm going to create a child theme of the Twenty Fourteen theme, with just two files
    I'll come back to this file later to add styling, but for now it's all WordPress needs to recognize the child theme.
    As I want my main blog page to display the latest post in each category, I'm going to create a new  file in my child theme.
    First I'll copy the  file from Twenty Fourteen and edit out the loop and other content so it looks like this:
    The first step is to identify the categories in your blog. Immediately after the opening tag, add the following:
    This uses the function to fetch a list of the categories in the blog. By default this will be fetched in alphabetical order and any empty categories won't be included. This works for me so I'm not adding any extra arguments.
    Then I'm using  to tell WordPress to run through each of these categories in turn and run the code inside the braces. The next step will be to create a query which will run for each of those categories.
    Now you need to define the arguments for your query. Inside the braces, add this:
    This will fetch just one post in the current category.
    This will output the featured image, title and excerpt for each post, with each enclosed in a link.
    Let's take a look at how this looks now:
    As you can see, there's a problem. My page is displaying the most recent post in each category, but it's duplicating posts because sometimes a post will be the most recent post in more than one category. Let's fix that.
    Above the line where you added the function, add this line:
    Next, add a new line below the opting of your query, so the first two lines look like this:
    This adds the ID of the current post to the array.
    Finally, add a new argument to your query arguments to avoid outputting any posts in this array. Your arguments will now look like this:
    This uses the  argument which looks for an array of post IDs.
    Save your file and take a look at your blog page again:
    That's better! Now your posts aren't duplicated.
    At the moment it's all a bit spread out, with the featured images above the post title and excerpt. Let's add some styling to float the image to the left.
    In your theme's  file, add the following:
    Now the content fits more nicely onto the page and is laid out better
Display the most Recent Post from a Specific Category Reviewed by Unknown on 2:16 AM Rating: 5

No comments:

All Rights Reserved by Boca Raton Real Estate © 2014 - 2015
Powered By Blogger, Designed by Sweetheme

Contact Form

Name

Email *

Message *

Powered by Blogger.