Display related blog post by blog post category by making a function

Display related blog post by blog post category by making a function. which help you to put this as a related posts for a particular template.

send $ppp as a post per page and $cat for category slug.

function display_related_blog_posts($ppp,$cat)
{
global $post;
$args = array( ‘posts_per_page’ => $ppp, ‘category_name’=> $cat );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<div class=”col-md-6″>
<div class=”thumbnailList box effect4″>
<div class=”blog-heading”>
<a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a>
</div>
<div class=”entry-meta”>
<div class=”pull-left”><?php echo get_the_date(); ?></div>
<div class=”pull-left”>&nbsp;&nbsp;<span style=”color:#bbb;”>By</span>&nbsp;<?php echo get_the_author(); ?></div>
<div class=”pull-left”>&nbsp;&nbsp;&nbsp;|&nbsp;<?php comments_number( ‘no comments’, ‘one comment’, ‘% comments’ ); ?></div>
</div><!– .entry-meta –>
<div class=”clearfix”></div></br>
<?php the_post_thumbnail(‘thumb’); ?>
<?php the_excerpt(); ?>
<a href=”<?php the_permalink() ?>” class=”btn btn-default”>Read more</a>
</div>

</div>
<?php endforeach;
}

 

Leave a Comment