Jan 6, 2009 - PHP, Wordpress    12 Comments

WordPress: display posts by category ID. Solution with code & example.

Hello Guy!

Often in many blogging sites, you will see two or three columns of the most important categories. Recently I had to do this for a blog. It was a little bit cumbersome and time consuming. So, I would like to share the solution so that you can kill the time waste trying yourself in case you do not know how to do it. In this solution, I show 5 posts at maximum from my chosen three categories.

OK.
Let us start and let me show how to do it.

Step One: See the sample what would be the output

In the above image, you will notice that the first category has enough posts, so it can fetch 5 (five) posts for us. But in second column and third column, there are only three posts because those categories do not have more than three posts. And what I want to say at this point is – although we will try to get 5 posts from each category, we will not get enough in case that category has less than 5 (five) posts and this is done automatically.

Step Two: Adding custom function

Now open the functions.php file inside your theme directory and insert these two functions. If there is none, create a functions.php file. And at this point, please remember that all the functions and coding in this file are automatically added during the code execution.

Note that the function names are self descriptive.

Step Three: Embed code in your file(s)

Now open your footer.php file or any other file and add this code. In 99% cases, you may need to customize this small piece of code. The first three lines are category IDs.

Step Four: Link your CSS code

I have put the above CSS code in my style.css file. In 99% cases, you may need to customize according to your requirements.

Step Five: Test everything by opening the link

Now test you code and we are done!

Thank you for reading.

12 Comments

  • Hi there,

    I m very new to wordpress. I dont understand if its a page you build like hubpage for instance or if its a software like dreamweaver?

    Thanks

  • @John

    WordPress is a software itself
    if you download it and install, you can create posts and pages using this software from admin panel.

    my suggestion is download the software and play with it for some time.

    thank you.

  • $outputString = ”;

    is missing a quote..should be:

    $outputString = “”;

    Great code, thanks.

  • @ Chris

    Your code is correct and so is mine too.

    I used two single quotes and you used two double quotes.

    single quotes are faster than double quotes.

  • Yeah..but just so you know it copies over a single double quote which caused me problems.

    Regardless, great code — thanks for sharing!

  • @ Chris,

    OK.
    I got it.

    And you are always welcome.

  • Not working for me … I get all posts instead.

  • @ fano

    Please check that you category ID is correct. If you give a category ID that does not exist, you may get all the posts.

  • I’m getting all the posts too. All the IDs are correct. Any ideas?

  • It is very good and very helpful.
    Thanks a lot

  • Great help for me. Thank you very much. World needs people like you 😀

  • Great! It’s working fine for me.

    Thanks you, admin!

Got anything to say? Go ahead and leave a comment!