最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

display a widget on specific category and its sub categories

programmeradmin1浏览0评论

I'm not good at wordpress coding. but i want to display a widget on specific category with its sub categories and all child posts.

Is there any simple way for this purpose ?


I found out the "Widget Logic" plugin on this link : /

This plugin do exactly what i want.

I'm not good at wordpress coding. but i want to display a widget on specific category with its sub categories and all child posts.

Is there any simple way for this purpose ?


I found out the "Widget Logic" plugin on this link : https://wordpress/plugins/widget-logic/

This plugin do exactly what i want.

Share Improve this question edited Aug 8, 2018 at 13:43 Milad asked Aug 6, 2018 at 19:54 MiladMilad 13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

you can test category identifier and parent in a widget with that :

class WidgetExample extends \WP_Widget
{

    public const IDENTIFIANT = "example1";
    public const CATEGORY_ID = 25;

    public function __construct()
    {
        parent::__construct(
              self::IDENTIFIANT
            , "Example"
        );
    }

    public function widget($args, $instance)
    {

        if (!is_category()) {
            return;
        }

        $currentCategory = get_queried_object();

        if (    (self::CATEGORY_ID !== $currentCategory->ID)
            &&  (self::CATEGORY_ID !== $currentCategory->parent)
        ) {
            return;
        }


        echo "We are on category " . self::CATEGORY_ID;


    }

    public function form($instance) {
    }

}
发布评论

评论列表(0)

  1. 暂无评论