Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save developer-anuragsingh/21888f95c92bf3d0d570 to your computer and use it in GitHub Desktop.
Save developer-anuragsingh/21888f95c92bf3d0d570 to your computer and use it in GitHub Desktop.
Display wordpress posts only from a particular category
<?php
$cat_name = 'Abc'; # Set the category name
$args = array ( # Define Arguments want to supply
'category_name' => $cat_name,
'posts_per_page' => 2,
);
$query = new WP_Query($args); # Object initialization of WP_query
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();?>
<h1><?php the_title(); ?></h1>
<?php if(has_post_thumbnail()) : # check thumbnail is assigned to post or not
the_post_thumbnail('thumbnail', array('class'=>'abc')); # If assigned, then display thumbnail and set classes for image
endif; ?>
<?php the_content(); ?>
<?php
endwhile;
endif;
wp_reset_postdata(); # Restore the global $post variable of the main query loop
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment