Provide zblog template_zblog them_wordpress template download and customization

Wordpress related article function code example

Tianxing Studio 2022-02-13 16:44 Wordpress Tutorial one thousand six hundred and seventy-five 0 Comments


I wrote an article before“ How is the relevant article of zblogphp called? Introduction to zblog related articles ”This article introduces WordPress's Related articles Function implementation code.

First of all, let's explain the logic of this related article. Whether it is zblog or wordpress, the article's relevance is related through the tag tag. If two articles have the same tag tag, the related article module of one article will display another article.

Function code of related articles directly on wordpress:

 <?php $posttags = get_the_tags(); $category = get_the_category($post->ID); $the_cate_id = $category[0]->term_id; $xg_array = array(); if ($posttags) {     $tags = '';     foreach ($posttags as $tag) $tags . = $tag->term_id .  ',';     $args = array(         'post_status' => 'publish',         'tag__in' => explode(',', $tags),         'post__not_in' => explode(',', $post->ID),         'caller_get_posts' => 1,         'orderby' => 'comment_date',         'posts_per_page' => 10,     );     $xg_array = query_posts($args); } if (count($xg_array) == 0) {     $args = array(         'post_status' => 'publish',         'cat' => $the_cate_id,         'orderby' => 'comment_date',         'posts_per_page' => 10,     );     $xg_array = query_posts($args); } foreach ($xg_array as $related) {     echo ' <li><a href="' .  get_permalink($related->ID) .  '" title="' .  $related->post_title .  '">' .  $related->post_title .  '</a></li>'; } wp_reset_query(); ?>

The above code uses the "query_posts" function. The idea is to first obtain the tag tag of the current article, and then query other articles with the same tag through the tag tag; If there is no other article with the same label, the latest article under the category of the current article will be displayed.

PS: Modify the number 10 in the code to adjust the maximum quantity displayed.

The above is an example of WordPress related article function implementation code. If you have other questions or have different views on the above code, please leave a message in the comment area below the article.


Can't find a tutorial that can solve your problem?

You can try to search or ask questions directly online. We also provide charging technical support. If you need it, you can contact us online.

Online questions Online Service

welcome you Comment: Cancel Reply

 Please fill in the verification code
  • Latest articles
  • Hot article ranking
  • Most Comments
Label aggregation