WordPRess has many plug-ins to implement related article functions. The advantage of plug-ins is that they are simple to configure, but they may have a small impact on the speed of the website, so many people still prefer to use code to achieve the required functions, but then again, Code implementation also has disadvantages, that is, the configuration is complicated, and people who don't understand code are completely confused or can only copy other people's code. It is better to use plug-ins.
Here I have compiled several ways to use code to implement related articles. The functions of each part of the code will be clearly marked, as well as how to customize the functions you want. I hope it will be helpful to everyone. If you have any questions, you can post them to this article. Comment and I will reply to you promptly. Before we begin, let me explain that the HTML code format output by all the following methods is in the following form, and you can modify it as needed:
<ul id="xxx">
<li>* <a title="Article title 1" rel="bookmark" href="Article link 1">Article title 1</a></li>
<li>* <a title="Article title 2" rel="bookmark" href="Article link 2">Article title 2</a></li>
...
</ul>
Method 1: Tag related
First get all the tags of the article, and then get n articles under these tags, then these n articles are articles related to the article. All WordPress related article plug-ins that can be seen now use this method. Here is the implemented code:
<ul id="tags_related">
<?php
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
foreach ($post_tags as $tag)
{
// Get the tag list
$tag_list[] .= $tag->term_id;
}
// Randomly obtain a tag from the tag list
$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
// This method uses the query_posts() function to call related articles. The following is the parameter list
$args = array(
'tag__in' => array($post_tag),
'category__not_in' => array(NULL), // Category ID not included
'post__not_in' => array($post->ID),
'showposts' => 6, // Display the number of related articles
'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post(); update_post_caches($posts); ?>
<li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a ></li>
<?php endwhile; else : ?>
<li>* No related articles yet</li>
<?php endif; wp_reset_query(); } ?>
</ul>
Instructions for use: "Excluded category ID" means that related articles do not display articles under this category. Just change the NULL in the same row to the ID of the article category. Use half-width commas to separate multiple IDs. Because only 6 related articles are displayed here, no matter how many values are assigned to the parameter tag__in of query_posts(), only 6 articles under one tag will be displayed, unless the first tag has 1 article and the second tag has 2 articles, and the third one has 3 articles. . . . . . So if this article has multiple tags, then what we do is to randomly get the id of a tag, assign it to the tag__in parameter, and get the 6 articles under the tag.
Method 2: Classification related
This method achieves the purpose of obtaining related articles by obtaining the category id of the article, and then obtaining the articles under this category.
<ul id="cat_related">
<?php
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$cat = get_category( $cats[0] );
$first_cat = $cat->cat_ID;
$args = array(
'category__in' => array($first_cat),
'post__not_in' => array($post->ID),
'showposts' => 6,
'caller_get_posts' => 1);
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post(); update_post_caches($posts); ?>
<li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute();
?>"><?php the_title(); ?></a></li>
<?php endwhile; else : ?>
<li>* No related articles yet</li>
<?php endif; wp_reset_query(); } ?>
</ul>
Method 3: Tag related, SQL acquisition
The principle of obtaining related articles is similar to method 1, but when obtaining articles, SQL statements are used to directly read the database, thereby randomly obtaining 6 related article records, instead of the WordPress function query_posts().
<ul id="tags_related">
<?php
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
foreach ($post_tags as $tag)
{
// Get the tag list
$tag_list[] .= $tag->term_id;
}
// Randomly obtain a tag from the tag list
$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
$related = $wpdb->get_results("
SELECT {$wpdb->prefix}posts.post_title, {$wpdb->prefix}posts.guid
FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
WHERE {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id
AND {$wpdb->prefix}term_taxonomy.taxonomy = 'post_tag'
AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}
term_relationships.term_taxonomy_id
AND {$wpdb->prefix}posts.post_status = 'publish'
AND {$wpdb->prefix}posts.post_type = 'post'
AND {$wpdb->prefix}term_taxonomy.term_id = '" . $post_tag . "'
AND {$wpdb->prefix}posts.ID != '" . $post->ID . "'
ORDER BY RAND( )
LIMIT 6");
// The 6 in the above code is to limit the acquisition of only 6 related articles.
// By modifying the number 6, you can modify the number of articles you want
if ( $related ) {
foreach ($related as $related_post) {
?>
<li>* <a href="<?php echo $related_post->guid; ?>" rel="bookmark"
title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li>
<?php } } else { ?>
<li>* No related articles yet</li>
<?php } }?>
</ul>
Method 4: Classification related, SQL acquisition
The principle of obtaining related articles is similar to method 2, but when obtaining articles, SQL statements are used to directly read the database, thereby randomly obtaining 6 related article records, instead of the WordPress function query_posts().
<ul id="cat_related">
<?php
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$cat = get_category( $cats[0] );
$first_cat = $cat->cat_ID;
$related = $wpdb->get_results("
SELECT wp_posts.post_title, wp_posts.guid
FROM wp_posts, wp_term_relationships, wp_term_taxonomy
WHERE wp_posts.ID = wp_term_relationships.object_id
AND {$wpdb->prefix}term_taxonomy.taxonomy = 'category'
AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
AND {$wpdb->prefix}posts.post_status = 'publish'
AND {$wpdb->prefix}posts.post_type = 'post'
AND {$wpdb->prefix}term_taxonomy.term_id = '" . $first_cat . "'
AND {$wpdb->prefix}posts.ID != '" . $post->ID . "'
ORDER BY RAND( )
LIMIT 6");
if ( $related ) {
foreach ($related as $related_post) {
?>
<li>* <a href="<?php echo $related_post->guid; ?>" rel="bookmark"
title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li>
<?php } } else { ?>
<li>* No related articles yet</li>
<?php } }?>
</ul>
Method 5: Author related
This method is to obtain other articles by the author of this article to serve as related articles. The code is as follows:
<ul id="author_related">
<?php
$post_author = get_the_author_meta( 'user_login' );
$args = array(
'author_name' => $post_author,
'post__not_in' => array($post->ID),
'showposts' => 6, // Display the number of related articles
'orderby' => date, // Sort by time
'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post(); update_post_caches($posts); ?>
<li>* <a href="<?php the_permalink(); ?>" rel="bookmark"
title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; else : ?>
<li>* No related articles yet</li>
<?php endif; wp_reset_query(); ?>
</ul>
Time efficiency comparison
We will use a previous PHP code to measure the code execution time of each of the above related articles in order to evaluate the efficiency of each of the above methods and provide a reference for your choice. The following is to obtain 6 related articles in the same article. The final calculation time of each method above is as follows:
Method 1: 0.18067908287048 seconds
Method 2: 0.057158946990967 seconds
Method three: 0.037126064300537 seconds
Method 4: 0.045628070831299 seconds
Method five: 0.023991823196411 seconds
Original text: http://www.ludou.org/how-to-generate-related-posts-in-wordpress.html