BLOG

“I still suggest you call
this page 42Lab.
Works for both of us!”
- Joey

WordPress 101: Tips, Tricks & Hacks

In this WordPress 101 tutorial, we will teach you how you can show your visitors related posts to what they are browsing. Every website on the internet has experienced visitor stagnation at some point. A decreasing number of visitors on your pages can also result in the reduction of Adsense revenue. 

By showing related posts to your visitors, you engage them with content-types they are already interested in. With the help of related posts on WordPress website, you will be able to reduce your bounce rate while increasing the user engagement you get. 

Therefore to increase your visitor engagement, you must learn how to add related posts on your WordPress website. In our tutorial, we will teach you a method that will enable you to add related posts functionality to your site without a plugin.  

Add the following code to a single file to show related posts on WordPress without a plugin.

<?php  // - for use in the loop, list 5 post titles related to the first tag on current post:

  $backup = $post;  // backup the current object

  $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,

      'caller_get_posts'=>1related posts on your WordPress 

    );

    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) {

      while ($my_query->have_posts()) : $my_query->the_post(); ?>

        <h3><a href="/<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>

      <?php endwhile;

    } else { ?>

      <h2>No related posts found!</h2>

    <?php }

  }

  $post = $backup;  // copy it back

  wp_reset_query(); // to use the original query again

?>

 

42 works

Exploring the Future of Apps with Apple

Read Blog

Dream Bigger. Work Greener.
Explore Our New & Sustainable Workspace

READ POLICY
42 works
Get 42Works Blog updates in your Inbox!

    Phone Button +91-9517770042 whatsapp Button Chat With Us
    Ohio United States 3.16.51.137 https://42works.net/to-show-related-posts-in-wordpress/