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 : https://wordpress.org/plugins/widget-logic/

This plugin do exactly what i want.

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) {
    }

}
  • Thanks but i can't underestand the code. I'm using a commercial theme , and i want to display an Ad on specific category with all subcategories and all posts on those categories. but i'm not a i'm not a programmer. – Milad Jafari Aug 7 '18 at 10:44
  • I found out the "Widget Logic" plugin on this link : wordpress.org/plugins/widget-logic This plugin do exactly what i want. – Milad Jafari Aug 8 '18 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.