Home>Article>CMS Tutorial> How does WordPress implement related article functions? Several ways to share
How does WordPress implement the related article function? The following article will introduce to you several ways to implement related articles in WordPress code. I hope it will be helpful to you!
Many WordPress plug-ins can realize the functions of related articles. The advantage of plug-ins is that they are simple to configure, but they may have some small impact on the speed of the website, so many people still prefer I like to use code to implement the required functions, but then again, code implementation also has shortcomings, that is, the configuration is complicated, and people who don’t understand code are completely confused or can only copy other people’s code, so 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 detailed and how to customize the functions you want. I hope it will be useful to everyone. help. Before we start, let me explain that the HTML code format output by all the following methods is in the following form. You can modify it according to your needs:
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. The following is the implemented code:
Instructions for use: "Category ID not included" refers to related articles that do not display articles under this category. Just change the NULL in the peer to the ID of the article category. More The IDs are separated by half-width commas. 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 that tag.
This method achieves the purpose of obtaining related articles by obtaining the classification id of the article, and then obtaining the articles under this category.
The principle of obtaining related articles is similar to method one, but when obtaining articles, Use SQL statements to directly read the database, thereby randomly obtaining 6 related article records, instead of the WordPress function query_posts().
The principle of getting related articles is similar to method two, but when getting articles, SQL statements are used to directly read the database, thereby randomly obtaining 6 related article records, instead of using WordPress functions. query_posts().
This method is to obtain other articles of the author of the article to serve as related articles, code As follows:
We measure the code execution time of each of the above related articles in order to evaluate the efficiency of each of the above methods. , provide 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 one: 0.18067908287048 seconds
Method two: 0.057158946990967 seconds
Method three: 0.037126064300537 seconds
Method 4: 0.045628070831299 seconds
Method 5: 0.023991823196411 seconds
Recommended study: "WordPress Tutorial"
The above is the detailed content of How does WordPress implement related article functions? Several ways to share. For more information, please follow other related articles on the PHP Chinese website!