首页 文章

显示最近的类别Wordpress小部件

提问于
浏览
1

我在使用显示最新类别的小部件时遇到问题 .

代码:https://ghostbin.com/paste/6cwa5 https://ghostbin.com/paste/rtnqa

它在主页面上显示正常,但是当我转到帖子页面时,它只显示页面的类别 .

此外,我必须强制使用 aside 标记关闭窗口小部件 .

我究竟做错了什么?我应该不使用 wp_get_recent_posts 功能吗?

为什么 array_key_exists('after_widget', $args) 不使用十二十五主题返回true?

2 回答

  • 0

    你为什么用 wp_get_recents_posts ?据我所知,从快速看,它与标准值的 get_posts() 并没有什么不同 .

    除此之外,您的第91和第92行可能是麻烦制造者 .

    foreach ($recent_posts as $post) {
    $categories = get_the_category($post->ID);
    

    我想, $post 真的与你正在看的当前帖子绑定 . 这在主页上并不重要,但是一旦你看到一篇文章,你的loop- $ post就会被blogpost- $ post覆盖(你知道我的意思吗?) .

    尝试将它们更改为不同于 $post 的内容,这可能会解决您的问题 .

  • 1
    foreach ($recent_posts as $post) {
    $categories = get_the_category($post->ID);
    

    相反,你可以使用,这个下面 .

    因为$ post是全局可变的 .

    例:

    $categories = get_the_category($post->ID);
    
        foreach ($categories as $cat) {
    
            echo $cat->cat_name;
        }
    

相关问题