WordPress相关文章改为调用当前栏目文章列表的方法

橘子网 3,675 0

WordPress相关文章列表调用当前栏目自身文章列表的方法,排出其他目录,有利于相关性文章聚合展示,利于用户体验。

<?php
if ( is_single() ) :
global $post;
$categories = get_the_category();
foreach ($categories as $category) :
    ?>
   
        <?php
        $posts = get_posts('numberposts=5&category='. $category->term_id.'&exclude='.get_the_ID());
        foreach($posts as $post) :
        ?>
            <li>
            <a href="<?php%20the_permalink();%20?>"><?php the_title(); ?></a>
            </li>
        <?php endforeach; ?>
    
<?php
endforeach; endif ; ?>

使用方法:

将以上代码放在文章结尾或者其他位置,打开文章页模板,在想要展示的位置添加以上代码即可。以上代码调用的是5篇,可自行调整数值调用文章数量。

上一篇:

下一篇:

相关阅读

分享