


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:
<ul id="xxx"> <li>* <a title="文章标题1" rel="bookmark" href="文章链接1">文章标题1</a></li> <li>* <a title="文章标题2" rel="bookmark" href="文章链接2">文章标题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. The following is the implemented code:
<ul id="tags_related"> <?php global $post; $post_tags = wp_get_post_tags($post->ID); if ($post_tags) { foreach ($post_tags as $tag) { // 获取标签列表 $tag_list[] .= $tag->term_id; } // 随机获取标签列表中的一个标签 $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ]; // 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表 $args = array( 'tag__in' => array($post_tag), 'category__not_in' => array(NULL), // 不包括的分类ID '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 } } else { echo '<li>* 暂无相关文章</li>'; } wp_reset_query(); } else { echo '<li>* 暂无相关文章</li>'; } ?> </ul>
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.
Method 2: Classification related
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.
<ul id="cat_related"><?phpglobal $post;$cats = wp_get_post_categories($post->ID);if ($cats) { $args = array( 'category__in' => array( $cats[0] ), '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 } } else { echo '<li>* 暂无相关文章</li>'; } wp_reset_query(); }else { echo '<li>* 暂无相关文章</li>';}?></ul>
Method three: Tag related, SQL acquisition
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().
<ul id="tags_related"><?phpglobal $post, $wpdb;$post_tags = wp_get_post_tags($post->ID);if ($post_tags) { $tag_list = ''; foreach ($post_tags as $tag) { // 获取标签列表 $tag_list .= $tag->term_id.','; } $tag_list = substr($tag_list, 0, strlen($tag_list)-1); $related_posts = $wpdb->get_results(" SELECT DISTINCT ID, post_title FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy WHERE {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id AND ID = object_id AND taxonomy = 'post_tag' AND post_status = 'publish' AND post_type = 'post' AND term_id IN (" . $tag_list . ") AND ID != '" . $post->ID . "' ORDER BY RAND() LIMIT 6"); // 以上代码中的 6 为限制只获取6篇相关文章 // 通过修改数字 6,可修改你想要的文章数量 if ( $related_posts ) { foreach ($related_posts as $related_post) {?> <li><a href="<?php echo get_permalink($related_post->ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li><?php } } else { echo '<li>暂无相关文章</li>'; } }else { echo '<li>暂无相关文章</li>';}?></ul>
Method 4: Classification related, SQL Get
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().
<ul id="cat_related"><?phpglobal $post, $wpdb;$cats = wp_get_post_categories($post->ID);if ($cats) { $related = $wpdb->get_results(" SELECT post_title, ID 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 = '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 = '" . $cats[0] . "' AND {$wpdb->prefix}posts.ID != '" . $post->ID . "' ORDER BY RAND( ) LIMIT 6"); if ( $related ) { foreach ($related as $related_post) {?> <li>* <a href="<?php echo get_permalink($related_post->ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li><?php } } else { echo '<li>* 暂无相关文章</li>'; } }else { echo '<li>* 暂无相关文章</li>';}?></ul>
Method 5: Author related
This method is to obtain other articles of the author of the article to serve as related articles, code As follows:
<ul id="author_related"><?php global $post; $post_author = get_the_author_meta( 'user_login' ); $args = array( 'author_name' => $post_author, 'post__not_in' => array($post->ID), 'showposts' => 6, // 显示相关文章数量 'orderby' => date, // 按时间排序 '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 } } else { echo '<li>* 暂无相关文章</li>'; } wp_reset_query();?></ul>
Time efficiency comparison
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!

Do you want to move your blog from WordPress.com to WordPress.org? Many beginners start with WordPress.com but quickly realize their limitations and want to switch to the self-hosted WordPress.org platform. In this step-by-step guide, we will show you how to properly move your blog from WordPress.com to WordPress.org. Why migrate from WordPress.com to WordPress.org? WordPress.com allows anyone to create an account

Are you looking for ways to automate your WordPress website and social media accounts? With automation, you will be able to automatically share your WordPress blog posts or updates on Facebook, Twitter, LinkedIn, Instagram and more. In this article, we will show you how to easily automate WordPress and social media using IFTTT, Zapier, and Uncanny Automator. Why Automate WordPress and Social Media? Automate your WordPre

Just a few days ago, one of our users reported an unusual problem. The problem is that he reaches the limit of custom menu items. Any content he saves after reaching the menu item limit will not be saved at all. We've never heard of this issue, so we decided to give it a try on our local installation. More than 200 menu items were created and saved. The effect is very good. Move 100 items to the drop-down list and save them very well. Then we knew it had to do with the server. After further research, it seems that many others have encountered the same problem. After digging deeper, we found a trac ticket ( #14134 ) that highlighted this issue. Read very

Do you need to add custom metafields to custom taxonomy in WordPress? Custom taxonomy allows you to organize content besides categories and tags. Sometimes it is useful to add other fields to describe them. In this article, we will show you how to add other metafields to the taxonomy they create. When should custom metafields be added to custom taxonomy? When you create new content on your WordPress site, you can organize it using two default taxonomy (category and tag). Some websites benefit from the use of custom taxonomy. These allow you to sort content in other ways. For example,

Windows live writer is a versatile tool that allows you to post posts directly from your desktop to your WordPress blog. This means you don't need to log in to the WordPress admin panel to update your blog at all. In this tutorial, I will show you how to enable desktop publishing for your WordPress blog using Windows Live Writer. How to set up Windows Live Writer on WordPress Step 1: To use Windows Live Writer in WordPr

Recently, one of our users reported a very strange installation problem. When writing a post, they can’t see anything they write. Because the text in the post editor is white. What's more, all the visual editor buttons are missing, and the ability to switch from visual to HTML doesn't work either. In this article, we will show you how to fix the white text and missing button issues in the WordPress visual editor. Be a Beginner Note: If you are looking for hidden buttons that may be seen in screenshots of other websites, you may be looking for a kitchen sink. You have to click on the kitchen sink icon to see other options such as underline, copy from word, etc.

Do you want to display avatars in user emails in WordPress? Gravatar is a network service that connects a user's email address to an online avatar. WordPress automatically displays visitors’ profile pictures in the comments section, but you may also want to add them to other areas of the site. In this article, we will show you how to display avatars in user emails in WordPress. What is Gravatar and why should I display it? Gravatar stands for globally recognized avatars, which allows people to link images to their email addresses. If the website supports

Do you want to change the default media upload location in WordPress? Moving media files to other folders can improve website speed and performance and help you create backups faster. It also gives you the freedom to organize your files in the way that suits you best. In this article, we will show you how to change the default media upload location in WordPress. Why change the default media upload location? By default, WordPress stores all images and other media files in the /wp-content/uploads/ folder. In this folder you will find children of different years and months


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor