简体   繁体   English

显示最新类别Wordpress小部件

[英]Display recent categories Wordpress widget

I'm having trouble with a widget that displays the most recent categories. 我在显示最新类别的小部件时遇到了麻烦。

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

It is displaying fine on the main page but when I go to a post page, it only shows that pages' categories. 它在主页上显示良好,但是当我转到帖子页面时,它仅显示该页面的类别。

Also, I have to force the widget to close with the aside tag. 另外,我必须强制小部件使用aside标签关闭。

What am I doing wrong? 我究竟做错了什么? Should I not be using the wp_get_recent_posts function? 我应该不使用wp_get_recent_posts函数吗?

Why doesn't array_key_exists('after_widget', $args) return true using twelvefifteen theme? 为什么array_key_exists('after_widget', $args)使用十二点十五分主题返回true?

Why do you use wp_get_recents_posts ? 为什么使用wp_get_recents_posts As far as I can see from a quick look it´s not really different to get_posts() with standard values at all. 从快速浏览的角度来看,它与具有标准值的get_posts()并没有什么不同。

Besides that your lines 91 and 92 could be the troublemaker. 除此之外,您的91和92行可能是麻烦制造者。

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

I think, $post is really bound to the current post you are looking at. 我认为$post确实与您正在查看的当前帖子绑定。 That is no big thing on the homepage, but once you view an article, your loop-$post could be overwritten by the blogpost-$post (you know what I mean?). 在首页上这没什么大不了的,但是一旦您查看了一篇文章,您的loop- $ post可能会被blogpost- $ post覆盖(您知道我的意思吗?)。

Try changing those to something different than $post , that might solve your problem. 尝试将其更改为不同于$post ,这可能会解决您的问题。

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

insteadof you can use, this one belown. 而不是您可以使用,此下面。

because $post is a global varaiable. 因为$ post是全局变量。

Example: 例:

    $categories = get_the_category($post->ID);

    foreach ($categories as $cat) {

        echo $cat->cat_name;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM