WordPress code to implement related articles

WordPress has a lot of plug-ins to achieve the relevant features of the article, the advantages of plug-in configuration is simple, but the speed of the site may cause some small impact, so many people still prefer to use code to achieve the required functions, but then again, Code to achieve there are shortcomings, is the configuration of complex, do not understand the code completely puzzled or can only copy someone else’s code, not as good as plug-ins.
Here I have compiled several code to achieve the relevant article method, which will detail the role of the various parts of the code, and how to customize the function you want, we hope to help, what questions can be published in this article Comments, I will reply to you in time. Before you start, note that all of the following methods output the HTML code format are in the form that you can modify as needed:
<ul id = “xxx”>
<li> * <a title=”>> Curseys 1″ rel=”bookmark” href=” paper> Link 1″> Article Title 1 </a> </ li>
<li> * <a title=” pages of paper2″ rel=”bookmark” href=” paper> link 2″> article title 2 </a> </ li>

</ ul>

Method 1: Label related
First get all the labels of the article, and then get the n articles under these tags, then the n article is related to the article on the article. Now you can see the WordPress related article plugins are used this method. The following is the implementation of the 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 get a label in the tag list
$ post_tag = $ tag_list [mt_rand (0, count ($ tag_list) – 1)];
// The method uses the query_posts () function to call the related article, the following is the parameter list
$ args = array (
‘tag__in’ => array ($ post_tag),
‘category__not_in’ => array (NULL), // not included in the category ID
‘post__not_in’ => array ($ post-> ID),
‘showposts’ => 6, // show 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 </ li>
<? php endif; wp_reset_query ();}?>
</ ul>
Instructions for use: “does not include the classification of ID” refers to the article does not show the classification of the article, the peer NULL into the article can be classified ID, multiple ID comma separated by commas. Because the limit here only shows 6 related articles, so no matter to the query_posts () parameter tag__in how many values, are only show a label under the six articles, unless the first label has 1, the second label has 2, the third has 3. The The The The The So if this article has multiple tags, then we take the method is to randomly get a tag id, assigned to the tag__in this parameter, access to the six articles under the label.

Method 2: Classification related
The method is to obtain the article by the classification id, and then get the article under the category, to achieve the purpose of access to relevant articles.
<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 </ li>
<? php endif; wp_reset_query ();}?>
</ ul>