Last active
August 29, 2015 14:25
-
-
Save developer-anuragsingh/21888f95c92bf3d0d570 to your computer and use it in GitHub Desktop.
Display wordpress posts only from a particular category
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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