您可以使用以下内容在小部件中测试类别标识符和父项:
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) {
}
}