Blog Show Posts From One Category on WordPress Index.php
May 22, 2014 Development, jQuery, WordPress Social Share
You can find the full code block that I wrote below for the index.php file, but the first line of code is the one that restricts the page to one category. Using the category ID, this modification of the query_posts function is what did the trick.
<?php
query_posts('cat=3');
if(have_posts())
{
while(have_posts())
{
the_post();
?>
<div class="post">
<a href=""><h2></h2></a>
<p class="date"><?php echo get_the_date('M j, Y'); ?></p>
<?php
// Featured Image
if(has_post_thumbnail($post->ID))
{
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail');
?>
<div class="post-image">
<a href="<?php the_permalink(); ?>"><img src="" /></a>
</div>
<?php
}
the_excerpt();
?>
<a href="<?php the_permalink(); ?>" class="read-more">Read more</a>
<br class="clear" />
</div>
<?php
}
}
?>
There are a lot of other attributes you can pass through the query_posts function such as limiting the number of posts that print out per page and the sort order of the posts. In this case, it’s an easy way to modify the while loop and print out select posts.