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

wordpress 添加文章浏览历史功能

高版本的php不支持preg_replace,需要将

  1. preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", stripslashes($_COOKIE["WP-LastViewedPosts"]))

替换为:

  1. preg_replace_callback('!s:(\d+):"(.*?)";!', function($m) { return 's:'.strlen($m[2]).":\"$m[2]\";";}, stripslashes($_COOKIE["WP-LastViewedPosts"]))

修改完之后后台添加时报“无效值”错误,可以将

  1. add_action('widgets_init', 'zg_lwp_init');

及zg_lwp_init方法代码改为extends WP_Widget的方法来初始化小工具,代码如下:

  1. class last_viewed_posts extends WP_Widget {
  2. public function __construct() {
  3. $widget_ops = array(
  4. 'classname' => 'recently_viewed',
  5. 'description' => __( 'Last Viewed Posts' ),
  6. 'customize_selective_refresh' => true,
  7. );
  8. parent::__construct('last_viewed_posts', 'Last Viewed Posts', $widget_ops);
  9. }
  10. function widget($args, $instance) {
  11. extract($args);
  12. $title = apply_filters( 'widget_title', $instance['title'] );
  13. echo $before_widget;
  14. if ( ! emptyempty( $title ) )
  15. echo $before_title . $title . $after_title;
  16. ?>
  17. <div class="post_cat">
  18. <div id="recently-viewed">
  19. <?php if (function_exists('zg_recently_viewed')) { ?>
  20. <?php zg_recently_viewed(); ?>
  21. <?php } ?>
  22. </div>
  23. </div>
  24. <?php
  25. echo $after_widget;
  26. }
  27. function update( $new_instance, $old_instance ) {
  28. if (!isset($new_instance['submit'])) {
  29. return false;
  30. }
  31. $instance = $old_instance;
  32. $instance = array();
  33. $instance['title'] = strip_tags( $new_instance['title'] );
  34. return $instance;
  35. }
  36. function form($instance) {
  37. if ( isset( $instance[ 'title' ] ) ) {
  38. $title = $instance[ 'title' ];
  39. }
  40. else {
  41. $title = 'Last Viewed Posts';
  42. }
  43. ?>
  44. <p><label for="<?php echo $this->get_field_id( 'title' ); ?>">标题:</label>
  45. <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>
  46. <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
  47. <?php }
  48. }
  49. add_action( 'widgets_init', create_function( '', 'register_widget( "last_viewed_posts" );' ) );

本文已通过「原本」原创作品认证,转载请注明文章出处及链接。

夏日阳光
  • 本文由 夏日阳光 发表于 2019年3月10日
  • 本文为夏日阳光原创文章,转载请务必保留本文链接:https://www.pieruo.com/43.html
评论  4  访客  4
    • 天空SKY
      天空SKY 0

      感谢~升级后一直困扰的BUG在你这里解决了

        • 夏日阳光
          夏日阳光

          @ 天空SKY
          很开心能帮助到你。

            • 虫子君
              虫子君 0

              @ 夏日阳光 大佬你的这个代码高亮是插件还是代码实现的,可以共享一下吗,都是使用begin主题的

                • 夏日阳光
                  夏日阳光

                  @ 虫子君 我用的是 Crayon Syntax Highlighter 这个插件。

          匿名

          发表评论

          匿名网友
          :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
          确定

          拖动滑块以完成验证
          加载中...