检查的属性get_queried_object()
.
示例代码:
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: Current Term Widget
*/
add_action( \'widgets_init\', array ( \'Current_Term_Widget\', \'register\' ) );
class Current_Term_Widget extends WP_Widget
{
public function __construct()
{
parent::__construct( \'current_term\', \'Current Term\' );
}
public function widget( $args, $instance )
{
if ( isset ( get_queried_object()->taxonomy )
&& isset( get_queried_object()->name )
)
{
return print $args[\'before_widget\']
. \'<b style="padding:10px;border:3px solid red">\'
. get_queried_object()->name
. \'</b>\'
. $args[\'after_widget\'];
}
}
public static function register()
{
register_widget( __CLASS__ );
}
}
以下是类别存档的屏幕截图
Cat B 从
test data:
将小部件输出限制为分类比较get_queried_object()->taxonomy
使用您注册的分类名称。