Simple WordPress Post Loop



Published On Thursday June 4, 2020
Reading Time: < 1 minute

WordPress is all about posts and showing posts is possible with the help of loop. Though it looks like a simple php loop it is abit different since we are talking about a WordPress loop.

<?php
      $args = array(
      'posts_per_page' => '3',
      'order' => 'DESC',
      'orderby' => 'date',
      'cat' => -17, // - indicates the category that is not to be included
      'cat' => 35 // - indicates the category to be included
      //if no cat element present then all the post will enter the loop
);
     $query = new WP_Query( $args );
     if ( $query->have_posts() ) {
          while ( $query->have_posts() ) {
          $query->the_post();
?>
     <img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>"/>
     <li class="item">
          <a href="<?php the_permalink($post->ID); ?>"><?php echo get_the_title($post->ID); ?></a>
     </li>
<?php
 } 
}
wp_reset_postdata(); 
?>

 

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x