WordPress: show similar articles without plugins

Introduction

In this article, I propose a PHP function to display similar articles on your WordPress site.

Following the hacking of the plugin Yuzo Related Post that I used on the site, I finally decided to do without plugin to do that.

To have a demo, I invite you to browse the site to see the result.

The code

In the file functions.php of your WordPress theme add the function below:

//
// RDR : get related post
//
function get_rdr_related_post($post_id){
    $style_rp = 2;
    $tags = wp_get_post_tags($post_id);
    if ($tags) {
        
        //$first_tag = $tags[0]->term_id;
        $tag_ids = array();	
        foreach($tags as $individual_tag){
            $tag_ids[] = $individual_tag->term_id;
        } 

        $args=array(
            'tag__in' => $tag_ids,
            'post__not_in'      => array($post_id),
            'posts_per_page'    =>3,
            'caller_get_posts'  =>1,
            'meta_key'          => '_thumbnail_id',
            'orderby'           => 'rand',
        );
        $my_query = new WP_Query($args);

        if($style_rp == 1){
            ?> <div class="row"> <?php
            if( $my_query->have_posts() ) {
                while ($my_query->have_posts()) : $my_query->the_post(); ?>
                <div class="col">
                    <a href="<?php echo esc_url( get_permalink() ); ?>">
                        <?php the_post_thumbnail('medium', array('class' => 'rdrit-home-thumbnail-max200') ); ?>
                    </a>
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </div>
                <?php
                endwhile;
            }
            wp_reset_query();
            ?> </div> <?php
        }elseif($style_rp == 2){
            if( $my_query->have_posts() ) {
                while ($my_query->have_posts()) : $my_query->the_post(); ?>
                    <?php $excerpt = get_the_content(''); ?>

                    <div class="row">
                        <div class="col-2">
                            <div class="rdr-rp-image" style="background:url('<?php echo get_the_post_thumbnail_url(); ?>') 50% 50% no-repeat;width: 97.75px;height:68px;margin-bottom: 5px;background-size:  cover;"></div>
                        </div>
                        <div class="col-10">
                            <a class="rdr-rp-title" href="<?php the_permalink() ?>" style="font-size:12px;line-height:22px;"><?php the_title(); ?></a><br/>
                            <span class="rdr-tp-text" style="font-size:11px;"><?php echo wp_html_excerpt(strip_shortcodes($excerpt),250) ?></span>
                        </div>
                    </div><hr/>
                <?php
                endwhile;
            }
            wp_reset_query();
        }
        
    }
}

The display is based on the Bootstrap.

In the theme’s article display file, call the function with the following code:

<?php get_rdr_related_post($post->ID); ?>

Conclusion

If you have some knowledge in PHP, it’s easy to customize the function and have something you manage the code for.

I’m not against plugins, which is the strength of WordPress, but for some features, it is still better to do things even that which reduces the risk for your site.

A function of 60 lines (it could do less) will replace a plugin of almost 200 files without really knowing the content and how your site is affected by it.



Related Posts