WordPress: get posts/pages from the category or parent page

wordpress how to get a catefory posts

Are you customizing the wordpress and trying to add similar post on the specific location of the page or post. This turorial is helpful if you are fetching a numbers of posts lets say 3 or 5 or something from the specific category on the post page or you are trying to display others similar page of the specific parent on the some location of the page.

lets start the wordpress coding.
Getting the related post of the specific category

  1. Know the category ID

go the post -> Category -> Edit category -> Get the Tag_ID, here in mine, Tag_ID=4

  1. Now use the following codes to get three post in random order used orderby=rand
<?php
 $currentID = get_the_ID();
 $recent = new WP_Query( 'exclude=' . $currentID . '&cat=4&showposts=3&orderby=rand' );
 while ($recent->have_posts()) : $recent->the_post();
?>
 <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 topshift30">
 <a href="<?php the_permalink(); ?>"><div class="thumbimg">
 <?php //if ( has_post_thumbnail() ) { the_post_thumbnail( 'thumb' ); } ?>
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() );?>" width="100%" height="auto">
 </div></a>
 <div class="similarcategory posts">

 <h3 class="grey"><?php the_title(); ?></h3>
 <p class="hometxt"><?php the_excerpt(); ?></p>
 <a href="<?php the_permalink(); ?>"><span class="darkbluebg whitecolor smallbtn">MORE INFO</span></a>
 <div class="centered" style="height:30px; clear:both;"></div>
 <div class="clearfix"></div>
 </div>
 </div>
<?php endwhile; ?>
Filed under: Wordpress