Several methods for WordPress code implementation

Source: Internet
Author: User
Many WordPress plug-ins can implement the functions of relevant articles. the advantage of the plug-in is that the configuration is simple, but it may have a slight impact on the speed of the website, so many people prefer to use the code to implement the required functions, but the words are back.

Many WordPress plug-ins can implement the functions of relevant articles. the advantage of the plug-in is that the configuration is simple, but it may have a slight impact on the speed of the website, therefore, many people prefer to use code to implement the required functions, but once again, code implementation also has its disadvantages, that is, complicated configuration, people who do not understand the code are completely confused or can only copy others' code. it is better to use plug-ins.

Here I have compiled several methods to use code to implement relevant articles. this will detail the functions of each part of the code and how to customize the functions you want. I hope this will be helpful to you, if you have any questions, please comment on this article. I will reply to you in time. Before getting started, it should be noted that the HTML code format output by all of the following methods is in the following format. you can modify it as needed:

 
 
  • * Article title 1
  • * Article title 2
  • ......
Method 1: tag-related

First, obtain all the tags of the article, and then obtain n articles under these tags. then these n articles are related to the article. This method is used for WordPress-related article plug-ins. The following is the implementation code:

 
 
    ID); if ($ post_tags) {foreach ($ post_tags as $ tag) {// Obtain the tag list $ tag_list []. = $ tag-> term_id;} // randomly obtain a tag in the tag list $ post_tag = $ tag_list [mt_rand (0, count ($ tag_list)-1)]; // This method uses the query_posts () function to call relevant 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);?>
  • * "Rel =" bookmark "title =" ">
  • * No related articles

Usage: "excluded Category ID" indicates that the articles under this category are not displayed in the relevant articles. you can change the NULL value of the same row to the ID of the document category, multiple IDs are separated by commas. Because only six related articles are displayed here, no matter how many values are assigned to the tag _ in parameter of query_posts (), only six articles under one tag are displayed, except the first tag, the second tag, and the third tag ...... Therefore, if this article has multiple tags, we will randomly obtain the id of a tag, assign the tag _ in parameter, and obtain the six articles under the tag.

Method 2: Classification

This method is to obtain the Category id of the article and then the articles under the category to obtain the relevant articles.

 
 
    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);?>
  • * "Rel =" bookmark "title =" ">
  • * No related articles
Method 3: tag-related, obtained by SQL

The principles and methods for obtaining relevant articles are similar. However, when obtaining articles, you can directly read the database using SQL statements, so that you can obtain 6 relevant article records at random, instead of the WordPress function query_posts ().

 
 
    ID); if ($ post_tags) {$ tag_list = ''; foreach ($ post_tags as $ tag) {// get the tag list $ tag_list. = $ tag-> term_id. ',';} $ tag_list = substr ($ tag_list, 0, strlen ($ tag_list)-1); $ related_posts = $ wpdb-> get_results ("select distinct id, post_titleFROM {$ wpdb-> prefix} posts, {$ wpdb-> prefix} term_relationships, {$ wpdb-> prefix} term_taxonomyWHERE {$ wpdb-> prefix} term_taxonomy.term_taxonomy_id = {$ wpdb-> prefix} term_relationsh Ips. term_taxonomy_idAND ID = object_idAND 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"); // in the code above, only 6 related articles are restricted. // BY modifying the number 6, you can modify the number of articles you want. if ($ related_posts) {foreach ($ related_posts as $ related_post) {?>
  • ID);?> "Rel =" bookmark "title =" Post_title;?> "> Post_title;?>
  • No related articles
Method 4: Classification

The principle of getting related articles is similar to Method 2. However, when getting articles, you can directly read the database using SQL statements, so that you can obtain 6 related article records at random, instead of the WordPress function query_posts ().

 
 
    ID); if ($ cats) {$ related = $ wpdb-> get_results ("SELECT post_title, IDFROM {$ wpdb-> prefix} posts, {$ wpdb-> prefix} term_relationships, {$ wpdb-> prefix} term_taxonomyWHERE {$ wpdb-> prefix} posts. ID = {$ wpdb-> prefix} term_relationships.object_idAND {$ wpdb-> prefix} category = 'Category 'AND {$ wpdb-> prefix} category = {$ wpdb-> prefix} category {$ 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) {?>
  • * ID);?> "Rel =" bookmark "title =" Post_title;?> "> Post_title;?>
  • * No related articles
Method 5: Author-related

This method is to obtain other articles from the author of this article to act as relevant articles. the code is as follows:

 
 
    $ 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);?>
  • * "Rel =" bookmark "title =" ">
  • * No related articles


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.