How to Output Posts from a Specific WordPress Category

wordpress custom category outputdisplay specific category postswordpress query_posts exampleget category id wordpresswordpress php code snippet
Published·Modified·

Sometimes we need to output posts from a specific WordPress category to a designated location. How to achieve this? Actually, it is very simple; a single line of code can handle it.

wordpress-bg-medblue

Get Category ID

In the WordPress backend, go to Posts > Categories, find the category you need, and click to open it. For example, opening the "SEO Column" link in XiaoZ Blog. You will see the link address contains <code>tag_ID=745</code>, where 745 is the ID of the SEO Column. We need to record this ID for use later.

2016-12-03_144646

Add Code

Add the following code to the page where you want to display the posts. Adjust the category ID and the number of posts to display according to your actual needs.

<ul>
  <?php
    $args=array(
        'cat' => 745,   // Category ID
        'posts_per_page' => 10, // Number of posts to display
    );
    query_posts($args);
    if(have_posts()) : while (have_posts()) : the_post();
  ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
  <?php  endwhile; endif; wp_reset_query(); ?>
</ul>