Publish: 2015-10-14 | Modify: 2019-06-03
It is not difficult to implement related article recommendations for WordPress. For example, you can use Baidu Recommended, CNZZ Cloud Recommended, and some plugins to achieve this. This method is very simple, but it may have some impact on loading speed, especially for perfectionist webmasters.
Previously, Xiao Z's blog has been using CNZZ Cloud Recommended, which is generally satisfactory. You only need to obtain a piece of JavaScript code from the official website and add it to your website. If you need it, you can refer to the article "Adding CNZZ Cloud Recommended to Your Website to Enhance User Stickiness". However, Xiao Z likes to tinker, so he has abandoned CNZZ Cloud Recommended and switched to using code to implement related article recommendations. This method is more customizable and can improve loading speed. Friends who need it can refer to it.
The following code should be added to the appropriate location on the single.php page in the theme directory:
<ul id="cat_related">
<?php
global $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>* No related articles</li>';
}
wp_reset_query();
}
else {
echo '<li>* No related articles</li>';
}
?>
</ul>
After adding this code, the functionality is implemented, but it may look a bit ugly and not the desired effect. If you are familiar with CSS stylesheets, you can beautify it yourself. The following is the display effect of related articles on Xiao Z's blog.
This article is referenced from: "Several Methods to Generate Related Posts in WordPress".
I come from China and I am a freelancer. I specialize in Linux operations, PHP, Golang, and front-end development. I have developed open-source projects such as Zdir, ImgURL, CCAA, and OneNav.