Two ways to add related articles in WordPress without plugins

Two ways to add related articles in WordPress without plugins


The power and ease of use of WordPress lies in its countless themes and plug-ins, but too many plug-ins will affect the speed and security of the website. If you can use simple code to achieve the required functions, it is better to use code.
At the end of each article, displaying related articles can better retain customers.
Here are two methods:

The first

In the functions.php file of the theme you are using, add the following code

function wp_get_related_posts()<br>{<br>global $wpdb, $post,$table_prefix;<br>$limit = 10; //How Many Related Posts Displayed<br>if(!$post->ID){return;}<br>$now = current_time('mysql', 1);<br>$tags = wp_get_post_tags($post->ID);<br>$taglist = “'” . $tags[0]->term_id. “'”;<br>$tagcount = count($tags);<br>if ($tagcount > 1) {<br>for ($i = 1; $i < $tagcount; $i++) {<br>$taglist = $taglist . “, '” . $tags[$i]->term_id . “'”;<br>}<br>}<br>$limitclause = “LIMIT $limit”;<br>$q = “SELECT p.ID, p.post_title, p.post_date, p.comment_count, count(t_r.object_id) as cnt FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE t_t.taxonomy ='post_tag' AND t_t.term_taxonomy_id = t_r.term_taxonomy_id AND t_r.object_id = p.ID AND (t_t.term_id IN ($taglist)) AND p.ID != $post->ID AND p.post_status = 'publish' AND p.post_date_gmt < '$now' GROUP BY t_r.object_id ORDER BY cnt DESC, p.post_date_gmt DESC $limitclause;”;<br>$related_posts = $wpdb->get_results($q);<br>$output = “”;<br>if (!$related_posts)<br>{<br>$output .= '<li>No Related Posts</li>';<br>}<br>foreach ($related_posts as $related_post )<br>{<br>$dateformat = get_option('date_format');<br>$output .= '<li>';<br>$output .= '<a href=”'.get_permalink($related_post->ID).'” title=”'.wptexturize($related_post->post_title).' ('.mysql2date($dateformat, $related_post->post_date).')”>'.wptexturize($related_post->post_title).'</a> ('.$related_post->comment_count .')';<br>$output .= '</li>';<br>}<br>$output = '<h3>Related Posts</h3><ul>' . $output . '</ul>';<br>return $output;<br>}<br>function wp_related_posts_attach($content)<br>{<br>if (is_single()||is_feed())<br>{<br>$output = wp_get_related_posts();<br>$content = $content . $output;<br>}<br>return $content;<br>}<br>add_filter('the_content', 'wp_related_posts_attach',100);

The second

Add it to the single.php of the theme you are using, usually after <?php the_content(); ?>

<h3>Related Posts</h3><br><ul><br><?php<br>$tags = wp_get_post_tags($post->ID);<br>if ($tags) {<br>$first_tag = $tags[0]->term_id;<br>$args=array(<br>'tag__in' => array($first_tag),<br>'post__not_in' => array($post->ID),<br>'showposts'=>10,//Display 10 related posts<br>'caller_get_posts'=>1<br>);<br>$my_query = new WP_Query($args);<br>if( $my_query->have_posts() ) {<br>while ($my_query->have_posts()) : $my_query->the_post(); ?><br><li><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?phpthe_title_attribute(); ?>”><?php the_title();?> <?php comments_number(' ','(1)','(%)'); ?></a></li><br><?php<br>endwhile;<br>}<br>}<br>wp_reset_query();<br>?><br></ul>

The second method used directly by Aiba.

<<:  $2.5/month/256M memory/10G space/500G traffic/OpenVZ/VPS:BlueVM

>>:  Free domain name that supports DNS and domain name redirection: net.tf

Recommend

Tutorial on using AppFog free cloud space

About AppFog: AppFog is a PaaS platform based on ...

ManSora: $18.2/month/1GB RAM/10GB SSD space/1TB traffic/100Mbps port/KVM/UK CN2

ManSora, a Chinese merchant, mainly provides dedi...

VPSnet: $2.1/month/1GB RAM/10GB SSD space/unlimited traffic/OpenVZ/Lithuania

VPSnet, a Lithuanian hosting provider, was establ...

Automatically backup website files and databases and upload them to FTP space

#You need to modify the place from here MYSQL_USE...

HostKvm: 462 yuan/year/2GB memory/30GB space/400GB traffic/40Mbps/KVM/Singapore

HostKvm, a stable Chinese merchant under the Hong...

Talking nonsense about Tencent Cloud losing data

Original text : https://mp.weixin.qq.com/s/wcjvYp...

ChicagoVPS: $30/year/1CPU/2G/15G SSD/2T/1 IP/OpenVZ

ChicagoVPS has a lot of discounts recently, among...

OLVPS: 104 yuan/month/1GB memory/50GB space/2TB traffic/1Gbps port/KVM/Boli

OLVPS, that is, t667, the boss is a nice person. ...

Six Free Genesis Child Themes from StudioPress

I guess everyone who uses WP knows the name of St...

MICRONET VPS: $7.25/month/1GB RAM/20GB storage/100GB traffic/1 IP/India

MicroNet was founded in 2009, entered the UK mark...

Psychz: $79/month/12GB RAM/500GB hard drive/30TB traffic/29 IP/Los Angeles

Psychz, an American hosting provider, has its own...