Focus on cloud service provider activities
Notes on website operation and maintenance

Implementation of WordPress related article calling method without plug-in

I shared with Lao Zuo in front“ Reasons for abandoning the WordPress Related Posts plug-in "I wanted to use another" Yes Another Related Posts Plugin "plug-in. There are many foreign users and it is very popular. Matt Cutts also wrote about this plug-in in his blog Related articles Plug ins are recommended as the first choice. However, after using it, I found that everything else was good. It was the only place that could not be customized. It was displayed directly at the bottom of the article by default. If the article had content added at the bottom of the template, the content would be called below. So I still give up the use of plug-ins and directly adopt the implementation without plug-ins WordPress related Article calls method.

Step 1: Edit the following code where relevant content needs to be added:

<div class="post-relate">
<h3>You may like the following:</h3>
<ul>
<php
$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) ];
$args = array(
'tag__in' => array($post_tag),
'category__not_in' => array(NULL),
'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 : >
<php $ashu_cats = wp_get_post_categories($post->ID);
if( $ashu_cats ){
$args = array(
'category__in' => array( $ashu_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 endwhile; endif; wp_reset_query(); } >
<php endif; wp_reset_query(); } >
</ul>
<div class="clear"></div>
</div>

Step 2: Add style

If you feel that the interface is good after adding the above code, you can use it directly. If not, you need to use the following style.

/* post-relate */
.post-relate{padding:10px 5px;color:#888;}
.post-relate h3{margin:0; line-height:22px;Verdana;font-size:14px;}
.post-relate ul{margin:5px 15px;padding:0px 5px;}
.post-relate li{margin:0px 0px; padding:2px 0px 2px;}

The style also needs to be adjusted according to your own needs. In this way, we can achieve the effect without plug-ins and reduce the memory consumption of WP. Do not use plug-ins if possible.

Domain name host preferential information push QQ group: six hundred and twenty-seven million seven hundred and seventy-five thousand four hundred and seventy-seven Get preferential promotion from merchants.
Like( zero )
Do not reprint without permission: Lao Zuo's Notes » Implementation of WordPress related article calling method without plug-in


Scan the code to follow the official account

Get more news about webmaster circle!
Entrepreneurship, operation and new knowledge