user3482921
user3482921

Reputation: 11

Trying to display posts only from one category in wordpress

I am using the magnet theme and am trying to display posts from a single category on the homepage. I have read through several post but have had no luck using the suggested methods I found.

Here is the snippet of code from the home-page-grid.php file that appears to be adding posts to the homepage

   <!-- Start content -->
   <div class="grid_8" id="content">



   <div class="widget_container content_page"> 
                 <div class="post_list_medium_widget">
                <div class="post_list_medium_style1">

<?php
global $paged;

if ( get_query_var('paged') ) {
     $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
     $paged = get_query_var('page');
} else {
    $paged = 1;
}
$query = new WP_Query( array ( 'paged' => $paged, 'orderby' => 'date', 'order' => 'DESC' ) );
$row_count=0;
while ( $query->have_posts() ) {
$row_count++;
$query->the_post();
$post_id = get_the_ID();            

?>

Any thoughts as to what needs to be done to get this to display a single category and all its sub categories?

Upvotes: 1

Views: 72

Answers (2)

Arka
Arka

Reputation: 591

<?php 
 $catPost = get_posts(get_cat_ID("your_category_name")); //change this with your category
   foreach ($catPost as $post) : setup_postdata($post); ?>
       <div>
             <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
             <p><?php the_content(); ?></p>
       </div>
<?php  endforeach;?>

Upvotes: 0

Companjo
Companjo

Reputation: 1792

Try adding this array item: 'cat' => '14' . 14 is the category id

$query = new WP_Query( array ( 'paged' => $paged, 'cat' => '14', 'orderby' => 'date', 'order' => 'DESC' ) );

Upvotes: 1

Related Questions