这应该是@Milo回答的扩展
您的代码中有几个缺陷
您不应该使用extract()
. 很难调试,并导致意外输出。的所有实例extract()
已从核心中删除
PHP 5.2已经过时,因此5.3也已经过时。create_function()
是5.3之前的版本。这是一个巨大的安全风险eval()
这应该不惜一切代价避免。正确的方法就是function()
宁可使用WP_Query
代替get_posts()
. WP_Query
缓存结果,使其更快
类名称单词应以camelcase开头
正确使用打开和关闭php标记,避免过度使用echo
我已经重写了您的完整小部件,使其与当前Widget API. 您可以只进行要添加或删除的调整
/* ------------------------------------------------------------------------- *
* Most Commented Post Widget
/* ------------------------------------------------------------------------- */
class Show_Popular_Commented extends WP_Widget {
public function __construct() {
parent::__construct(
\'show_popular_commented\',
__( \'Popular Commented Posts\' ),
array( \'description\' => __( \'Show your popular commented posts.\' ),
)
);
$this->alt_option_name = \'show_popular_commented\';
add_action( \'save_post\', array($this, \'flush_widget_cache\') );
add_action( \'deleted_post\', array($this, \'flush_widget_cache\') );
add_action( \'switch_theme\', array($this, \'flush_widget_cache\') );
}
public function widget( $args, $instance ) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( \'widget_commented_posts\', \'widget\' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args[\'widget_id\'] ) ) {
$args[\'widget_id\'] = $this->id;
}
if ( isset( $cache[ $args[\'widget_id\'] ] ) ) {
echo $cache[ $args[\'widget_id\'] ];
return;
}
ob_start();
$title = ( ! empty( $instance[\'title\'] ) ) ? $instance[\'title\'] : __( \'Commented Posts\' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( \'widget_title\', $title, $instance, $this->id_base );
$number = ( ! empty( $instance[\'number\'] ) ) ? absint( $instance[\'number\'] ) : 5;
if ( ! $number ) {
$number = 5;
}
/**
* Filter the arguments for the Commented Posts widget.
*
* @since 1.0.0
*
* @see WP_Query::get_posts()
*
* @param array $args An array of arguments used to retrieve the commented posts.
*/
$query_args = [
\'posts_per_page\' => $number,
\'orderby\' => \'comment_count\',
];
$q = new WP_Query( apply_filters( \'comment_posts_args\', $query_args ) );
if( $q->have_posts() ) {
echo $args[\'before_widget\'];
if ( $title ) {
echo $args[\'before_title\'] . $title . $args[\'after_title\'];
}
while( $q->have_posts() ) {
$q->the_post(); ?>
<div class="commented_post">
<style type="text/css">
.commented_post {display:block;margin:10px 0;border-bottom:1px solid #DEDEDE;}
.commented_post h4{font-size:16px;clear:both;display:block;}
.commented_post p{font-size:13px;text-align:justify;line-height:18px;margin:10px 0;}
</style>
<header class="entry-header">
<?php the_title( \'<h1 class="entry-title"><a href="\' . esc_url( get_permalink() ) . \'" rel="bookmark">\', \'</a></h1>\' ); ?>
</header><!-- .entry-header -->
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
</div><!-- #post-## -->
<?php
}
wp_reset_postdata();
}
echo $args[\'after_widget\'];
if ( ! $this->is_preview() ) {
$cache[ $args[\'widget_id\'] ] = ob_get_flush();
wp_cache_set( \'widget_commented_posts\', $cache, \'widget\' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
$instance[\'number\'] = (int) $new_instance[\'number\'];
$this->flush_widget_cache();
$alloptions = wp_cache_get( \'alloptions\', \'options\' );
if ( isset( $alloptions[\'widget_comment_post\'] ) )
delete_option( \'widget_comment_post\' );
return $instance;
}
public function flush_widget_cache() {
wp_cache_delete( \'widget_commented_posts\', \'widget\' );
}
public function form( $instance ) {
$title = isset( $instance[\'title\'] ) ? esc_attr( $instance[\'title\'] ) : \'\';
$number = isset( $instance[\'number\'] ) ? absint( $instance[\'number\'] ) : 5;
?>
<p>
<label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'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 $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( \'number\' ); ?>"><?php _e( \'Number of posts to show:\' ); ?></label>
<input id="<?php echo $this->get_field_id( \'number\' ); ?>" name="<?php echo $this->get_field_name( \'number\' ); ?>" type="text" value="<?php echo $number; ?>" size="3" />
</p>
<?php
}
}
add_action( \'widgets_init\', function () {
register_widget( \'Show_Popular_Commented\' );
});