Display Related Posts without any Plugin

2
280
Display Related Posts without any Plugin

অনির্বাচিত টিউনার™

®╔═════════════════════ஜ۩۞۩ஜ══════════════════════╗®
✪░░▒▓███►✂✂((((☠☠➸Uη§є₤є©†єd✖Ŧwєє†єЯ™➸☠☠))))✂✂◄███▓▒░░✪ ®╚═════════════════════ஜ۩۞۩ஜ══════════════════════╝®

The Ultimate Path of The Bangla Technology
অ আ ক খ প্রযুক্তি এখন আমার ভাষায়
Display Related Posts without any Plugin

Welcome Back on “Unselected Tuner” ‘s blog. Howdy, I hope you are well. Today I give you a tweet on WordPress. Now see the tweet….

Problem with Displaying Related Posts without any Plugin

The problem i found is that, after the related posts were displayed at the end of the post, the comments displayed on the current page comes from the last post in the related posts list. I found this fault the next day only. So i thought of finding a fix for the code. I have made some changed to the codes and here are them.

Showing Related Posts without Plugin based an Categories

<?php categories = get_the_category($post->ID);
if ($categories) { $category_ids = array();   
foreach($categories as $individual_category)
$category_ids[] = $individual_category->term_id;
    $args=array(
        'category__in' => $category_ids,
        'post__not_in' => array($post->ID),
        'showposts'=>5, // Corresponds to Number of related posts to be shown.
        'caller_get_posts'=>1
    );
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Related Posts…..</h3><ol>';
while ($my_query->have_posts()) {
$my_query->the_post();?><li><a href="<?php the_permalink() ?>"
rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li><?php }
echo '</ol>'; } } $post = $backup; wp_reset_query(); ?>

Now paste the code in single.php ust before the post ends or just after <?php the_content(); ?>.<?php the_content(); ?>

Displaying Related Posts based on Tags without Plugin

<?php
$backup = $post;
$tags = wp_get_post_tags($post->ID);
$tagIDs = array();
if ($tags) {
$tagcount = count($tags);
for ($i = 0; $i < $tagcount; $i++) {
$tagIDs[$i] = $tags[$i]->term_id;
}
$args=array(
'tag__in' => $tagIDs,
'post__not_in' => array($post->ID),
'showposts'=>5, // Corresponds to Number of related posts to be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Related Posts…..</h3><ol>';
while ($my_query->have_posts()) {
$my_query->the_post();?><li><a href="<?php the_permalink() ?>"
rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li><?php }
echo '</ol>'; } } $post = $backup; wp_reset_query(); ?>

Paste the code just after the post content to display the related posts. You can change the value of ‘showposts’=>5 to a desired number. It corresponds to the number of related posts to be displayed.

Here is how the Related Posts are Displayed Based on Categories

If you have got some css code for the related posts code then just apply to this code easily. Just put this the whole code inside the div tag corresponding to the related posts css. [collected]

With best regards to all tweeters and visitiors. Be careful to you and Thanks to all. Allah Hafiz….

2 মন্তব্য

একটি উত্তর ত্যাগ