Skip to main content

This can be done using the WP_Query class. If all you're looking for is to grab all posts based on the category, you can do that using the ID or slug.

$args = array( 'cat' => 1, ); $new_query = new W_QueryWP_Query( $args ); 

OR

$args = array( 'cat_name' => 'news', ); $new_query = new W_QueryWP_Query( $args ); 

From there you rightwrite the loop as normal with one exception:

<?php if ( $new_query->have_posts() ) : ?> <?php while ( $new_query->have_posts() ) : $new_query->the_post(); ?> // Post Code Goes Here <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else : ?> // No Posts Found <?php endif; ?? 

You need to include the wp_reset_postdata(); function call which will reset the global $postglobal $post; back to the main query's value.

This can be done using the WP_Query class. If all you're looking for is to grab all posts based on the category, you can do that using the ID or slug.

$args = array( 'cat' => 1, ); $new_query = new W_Query( $args ); 

OR

$args = array( 'cat_name' => 'news', ); $new_query = new W_Query( $args ); 

From there you right the loop as normal with one exception:

<?php if ( $new_query->have_posts() ) : ?> <?php while ( $new_query->have_posts() ) : $new_query->the_post(); ?> // Post Code Goes Here <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else : ?> // No Posts Found <?php endif; ?? 

You need to include the wp_reset_postdata(); function call which will reset the global $post back to the main query's value.

This can be done using the WP_Query class. If all you're looking for is to grab all posts based on the category, you can do that using the ID or slug.

$args = array( 'cat' => 1, ); $new_query = new WP_Query( $args ); 

OR

$args = array( 'cat_name' => 'news', ); $new_query = new WP_Query( $args ); 

From there you write the loop as normal with one exception:

<?php if ( $new_query->have_posts() ) : ?> <?php while ( $new_query->have_posts() ) : $new_query->the_post(); ?> // Post Code Goes Here <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else : ?> // No Posts Found <?php endif; ?? 

You need to include the wp_reset_postdata(); function call which will reset the global $post; back to the main query's value.

Source Link
Cedon
  • 1.7k
  • 2
  • 13
  • 26

This can be done using the WP_Query class. If all you're looking for is to grab all posts based on the category, you can do that using the ID or slug.

$args = array( 'cat' => 1, ); $new_query = new W_Query( $args ); 

OR

$args = array( 'cat_name' => 'news', ); $new_query = new W_Query( $args ); 

From there you right the loop as normal with one exception:

<?php if ( $new_query->have_posts() ) : ?> <?php while ( $new_query->have_posts() ) : $new_query->the_post(); ?> // Post Code Goes Here <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else : ?> // No Posts Found <?php endif; ?? 

You need to include the wp_reset_postdata(); function call which will reset the global $post back to the main query's value.