WordPress related articles (non-plugin) implementation method

WordPress related articles (non-plugin) implementation method

Adding related articles after each log can achieve in-depth reading of related articles. The plug-in method is simple, but it seems cumbersome. The following is implemented with code:

<div class="related_posts"> <?php
$backup = $post;
$tags = wp_get_post_tags($post->ID);
$tagIDs = array();
if ($tags && count($tags)>1) {
echo '<h3>Related logs</h3>';
echo '<ul class="ul_style">';
$tagcount = count($tags);
for ($i = 0; $i < $tagcount; $i++) {
$tagIDs[$i] = $tags[$i]->term_id;
}
$args = array(
'tag__in' => $tagIDs,
'post__not_in' => array($post->ID),
'showposts'=>8,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title(); ?>” target=”_blank”> <?php the_title(); ?> </a></li>
<?php endwhile;echo '</ul>';} else { ?> <?php }} $post = $backup;wp_reset_query();?> </div>

Just add it at the end of single.php article.

<<:  $40/year/1024M memory/80G space/3TB traffic VPS —— BudgetVM

>>:  Godaddy Economy Web Hosting $1/month

Recommend

VMHaus 1GB RAM NVMe Hard Drive 10Gbps Port UK KVM VPS Review

Details: VMHaus: $15/year/256MB memory/5GB NVMe s...

$9/year/512M memory/5G space/500G traffic/OpenVZ VPS: BandwagonHost

BandwagonHost is a hosting provider founded in Se...

OVH: Dedicated servers, VPS, domain name discounts, good deals

OVH, a world-renowned hosting provider, has now l...

DeinServerHost: €3.95/month/1GB memory/300GB space/2TB traffic/Windows/Germany

DeinServerHost, a German hosting provider, was es...

[Black Friday] AlphaVPS: VPS in Los Angeles/UK, 128GB space, 15 euros per year

AlphaVPS, which has been introduced many times in...