如果想增加调用访客最近浏览过的文章功能,可以使用Last Viewed Posts这个插件,但由于这个插件很久没更新了,使用高版本的php及wordpress存在兼容性问题,可以按以下方式修改:

高版本的php不支持preg_replace,需要将
preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", stripslashes($_COOKIE["WP-LastViewedPosts"]))
替换为:
preg_replace_callback('!s:(\d+):"(.*?)";!', function($m) { return 's:'.strlen($m[2]).":\"$m[2]\";";}, stripslashes($_COOKIE["WP-LastViewedPosts"]))
修改完之后后台添加时报“无效值”错误,可以将
add_action('widgets_init', 'zg_lwp_init');及zg_lwp_init方法代码改为extends WP_Widget的方法来初始化小工具,代码如下:
class last_viewed_posts extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'recently_viewed',
'description' => __( 'Last Viewed Posts' ),
'customize_selective_refresh' => true,
);
parent::__construct('last_viewed_posts', 'Last Viewed Posts', $widget_ops);
}
function widget($args, $instance) {
extract($args);
$title = apply_filters( 'widget_title', $instance['title'] );
echo $before_widget;
if ( ! emptyempty( $title ) )
echo $before_title . $title . $after_title;
?>
<div class="post_cat">
<div id="recently-viewed">
<?php if (function_exists('zg_recently_viewed')) { ?>
<?php zg_recently_viewed(); ?>
<?php } ?>
</div>
</div>
<?php
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
if (!isset($new_instance['submit'])) {
return false;
}
$instance = $old_instance;
$instance = array();
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
function form($instance) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = 'Last Viewed Posts';
}
?>
<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 $title; ?>" /></p>
<input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
<?php }
}
add_action( 'widgets_init', create_function( '', 'register_widget( "last_viewed_posts" );' ) );
本文已通过「原本」原创作品认证,转载请注明文章出处及链接。




1F
感谢~升级后一直困扰的BUG在你这里解决了
B1
@ 天空SKY
很开心能帮助到你。
B2
@ 夏日阳光 大佬你的这个代码高亮是插件还是代码实现的,可以共享一下吗,都是使用begin主题的
B3
@ 虫子君 我用的是 Crayon Syntax Highlighter 这个插件。