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:

/**
 * Permet l'affichage d'article en relatation
 * Ajout de poste aleatoire sur pas de tag sur le post.
 * 
 * @param   int     $post_id    Post ID
 * @param   int     $per_page   Number of post print
 * @param   int     $style_rp   Result HTML
 * V2
 */
function get_rdr_related_post($post_id, $per_page = 3, $style_rp = 2){
    // Param Query
    $args=array(
        'post__not_in'      =>  array($post_id),
        'posts_per_page'    =>  $per_page,
        'caller_get_posts'  =>  1,
        'meta_key'          => '_thumbnail_id',
        'orderby'           => 'rand',
    );

    // Recuperation des tags
    $tags = wp_get_post_tags($post_id);

    if( $tags ){
        // post contenant des TAGS
        $tag_ids = array();	
        foreach($tags as $individual_tag){
            $tag_ids[] = $individual_tag->term_id;
        } 

        // Contruction filtre de la requete
        $args['tag__in'] = $tag_ids;
          
    }else{
        // Recuperation categories du post
        $cats = wp_get_post_categories($post_id);
        if($cats){
            $cats_ids = array();
            foreach($cats as $individual_cat){
                $cats_ids[] = $individual_cat->term_id;
            }
            $args['category__in'] = $tag_ids;
        } // /if($cats)
    } // /else

    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) {

        if($style_rp == 1){
            ?> <div class="row"> <?php            
                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><br/>
                    <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </div>
                <?php
                endwhile;           
            ?> </div> <?php
        }elseif($style_rp == 2){            
            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();
    } // /$my_query->have_posts()
} // end function

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.




Leave a Comment