WP_Widget_Archives
概述
WP_Widget_Archives 是 WordPress 核心类,用于实现归档小部件,显示网站文章的月度归档。它继承自 WP_Widget,提供小部件的创建、设置、更新和输出功能。
关键要点
- 继承自 WP_Widget,用于创建归档小部件实例。
- 包含四个主要方法:__construct() 初始化小部件,widget() 输出内容,update() 处理设置更新,form() 输出设置表单。
- 支持下拉菜单和显示文章计数选项,通过 wp_get_archives() 函数生成归档列表。
- 使用 WordPress 标准小部件钩子和过滤机制,如 widget_title 过滤器。
代码示例
public function __construct() {
$widget_ops = array(
'classname' => 'widget_archive',
'description' => __( 'A monthly archive of your site’s Posts.' ),
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
);
parent::__construct( 'archives', __( 'Archives' ), $widget_ops );
}注意事项
- 小部件设置包括标题、是否显示文章计数和是否使用下拉菜单,需通过表单和 update 方法处理。
- 输出内容时需遵循 WordPress 小部件结构,使用 before_widget 和 after_widget 等参数。
- 自 WordPress 2.8.0 版本引入,确保兼容性时注意版本要求。
Core class used to implement the Archives widget.
Description
See also
Methods
| Name | Description |
|---|---|
| WP_Widget_Archives::__construct | Sets up a new Archives widget instance. |
| WP_Widget_Archives::form | Outputs the settings form for the Archives widget. |
| WP_Widget_Archives::update | Handles updating settings for the current Archives widget instance. |
| WP_Widget_Archives::widget | Outputs the content for the current Archives widget instance. |
Source
class WP_Widget_Archives extends WP_Widget {/**
* Sets up a new Archives widget instance.
*
* @since 2.8.0
*/
public function __construct() {
$widget_ops = array(
'classname' => 'widget_archive',
'description' => __( 'A monthly archive of your site’s Posts.' ),
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
);
parent::__construct( 'archives', __( 'Archives' ), $widget_ops );
}/**
* Outputs the content for the current Archives widget instance.
*
* @since 2.8.0
*
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance Settings for the current Archives widget instance.
*/
public function widget( $args, $instance ) {
$default_title = __( 'Archives' );
$title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );$count = ! empty( $instance['count'] ) ? '1' : '0';
$dropdown = ! empty( $instance['dropdown'] ) ? '1' : '0';echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}if ( $dropdown ) {
$dropdown_id = "{$this->id_base}-dropdown-{$this->number}";
?>
<label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"></label>
<select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown">
'monthly',
'format' => 'option',
'show_post_count' => $count,
),
$instance
);switch ( $dropdown_args['type'] ) {
case 'yearly':
$label = __( 'Select Year' );
break;
case 'monthly':
$label = __( 'Select Month' );
break;
case 'daily':
$label = __( 'Select Day' );
break;
case 'weekly':
$label = __( 'Select Week' );
break;
default:
$label = __( 'Select Post' );
break;
}
?><option value=""></option>
</select>
<script>
( ( dropdownId ) => {
const dropdown = document.getElementById( dropdownId );
function onSelectChange() {
setTimeout( () => {
if ( 'escape' === dropdown.dataset.lastkey ) {
return;
}
if ( dropdown.value ) {
document.location.href = dropdown.value;
}
}, 250 );
}
function onKeyUp( event ) {
if ( 'Escape' === event.key ) {
dropdown.dataset.lastkey = 'escape';
} else {
delete dropdown.dataset.lastkey;
}
}
function onClick() {
delete dropdown.dataset.lastkey;
}
dropdown.addEventListener( 'keyup', onKeyUp );
dropdown.addEventListener( 'click', onClick );
dropdown.addEventListener( 'change', onSelectChange );
})( <?php echo wp_json_encode( $dropdown_id, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> );
</script>
';
}
?><ul>
'monthly',
'show_post_count' => $count,
),
$instance
)
);
?>
</ul>';
}
}echo $args['after_widget'];
}/**
* Handles updating settings for the current Archives widget instance.
*
* @since 2.8.0
*
* @param array $new_instance New settings for this instance as input by the user via
* WP_Widget_Archives::form().
* @param array $old_instance Old settings for this instance.
* @return array Updated settings to save.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$new_instance = wp_parse_args(
(array) $new_instance,
array(
'title' => '',
'count' => 0,
'dropdown' => '',
)
);
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['count'] = $new_instance['count'] ? 1 : 0;
$instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;return $instance;
}/**
* Outputs the settings form for the Archives widget.
*
* @since 2.8.0
*
* @param array $instance Current settings.
*/
public function form( $instance ) {
$instance = wp_parse_args(
(array) $instance,
array(
'title' => '',
'count' => 0,
'dropdown' => '',
)
);
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
</p>
<p>
<input class="checkbox" type="checkbox"<?php checked( $instance['dropdown'] ); ?> id="get_field_id( 'dropdown' ); ?>" name="get_field_name( 'dropdown' ); ?>" />
<label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"></label>
<br />
<input class="checkbox" type="checkbox"<?php checked( $instance['count'] ); ?> id="get_field_id( 'count' ); ?>" name="get_field_name( 'count' ); ?>" />
<label for="<?php echo $this->get_field_id( 'count' ); ?>"></label>
</p>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/widgets/class-wp-widget-archives.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/widgets/class-wp-widget-archives.php#L17">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/widgets/class-wp-widget-archives.php#L17-L247">View on GitHub</a></p></section><section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/classes/wp_widget/">WP_Widget</a><code>wp-includes/class-wp-widget.php
Core base class extended to register widgets.
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |