也谈WordPress说说/微博功能

姐姐做事情一向不喜欢从头开始搞,主要是水平太菜,搞php真的是不专业。于是就只能到处抄作业了。好在wp算是使用比较广泛的系统,相对来说查找各种资料还不算太麻烦,但是国内的互联网环境嘛,就一言难尽了。各种抄作业,基本是一篇文章被搬到了不同的地方。可能很多人连尝试都没尝试就抄过去了,但是姐姐不一样啊。姐姐的作业是要实际测试的。

之前看杜老师弄的说说还是蛮不错的,基于memos。不过个人觉得总有点遗憾,那就是两个系统是独立的,账号不互通。另外一个问题就是自己的这个说说模块基本都是自己用,也没有太大的必要搞多用户。于是就想着找找各种插件,测试了不少插件之后,最终觉得Simple microblogging这款插件基本能够满足需求。

不过在使用的过程中也发现了几个问题:

1.页面排版有点另类,具体可以直接安装插件后创建文章查看,横向排版,标题文字图片一排。

2.文章加载要么加载固定条数,要么全部加载,这个设置对于有强迫症的我不太友好

3.没有分页

昨晚用虚拟机上的wp改了一晚上,最终页面样式改的自己多少能看的过去了

但是分页的问题确实搞不定了,当时就想放弃了。主要是对于wp的各种函数也不熟悉啊,真是让人头大。于是想着淘宝上找个人给改一下:

奈何对面小哥哥回消息有点慢,在等待的过程中姐姐把分页的问题给解决了。

于是现在基本就可以用了,效果嘛最起码自己看的过去了。

安装插件之后修改插件代码,直接复制下面的代码即可:

<?php
/*
 * Plugin Name: Simple microblogging
 * Description: Use your wordpress site as a microblog; display the microposts in a widget or using a shortcode.
 * Version: 0.1
 * Author: Samuel Coskey, Victoria Gitman
 * Author URI: http://boolesrings.org
*/
/*
https://www.qiniu.com/qfans/qnso-21193676#comments
*/

// 处理分页
function remove_page_from_query_string($query_string)
{ 
    if (isset($query_string['name']) && $query_string['name'] == 'page' && isset($query_string['page'])) {
        unset($query_string['name']);
        // 'page' in the query_string looks like '/2', so i'm spliting it out
        @list($delim, $page_index) = explode('/', $query_string['page']);
        $query_string['paged'] = $page_index;
    }      
    return $query_string;
}
// I will kill you if you remove this. I died two days for this line 
add_filter('request', 'remove_page_from_query_string');

// following are code adapted from Custom Post Type Category Pagination Fix by jdantzer
function fix_category_pagination($qs){
    if(isset($qs['category_name']) && isset($qs['paged'])){
        $qs['post_type'] = get_post_types($args = array(
            'public' => true,
            '_builtin' => false
        ));
        array_push($qs['post_type'],'post');
    }
    return $qs;
}
add_filter('request', 'fix_category_pagination');

/*
 * Create the new post type
*/
add_action( 'init', 'create_micropost_type' );
function create_micropost_type() {
 register_post_type( 'micropost',
  array(
   'labels' => array(
    'name' => __( 'Microposts' ),
    'singular_name' => __( 'Micropost' ),
   ),
   'has_archive' => true,
   'menu_icon' => plugins_url( 'microblogging-icon.png', __FILE__ ),
   'menu_position' => 5,
   'public' => true,
   'rewrite' => array( 'slug' => 'microposts' ),
   'supports' => array( 'title', 'editor', 'author', 'comments' ),
// uncomment to support categories and tags:
 'taxonomies' => array ( 'category', 'post_tag' ),
  )
 );
}
/*
 * Tells wordpress to reset its permalink structure, to accommodate the new post type
*/
register_activation_hook( __FILE__, 'my_rewrite_flush' );
function my_rewrite_flush()
{
 create_micropost_type();
 flush_rewrite_rules();
}
/*
 * Microblog widget code
*/
add_action('widgets_init', 'ahspfc_load_widgets');
function ahspfc_load_widgets() {
 register_widget('microblog_widget');
}
class microblog_widget extends WP_Widget {
 function microblog_widget() {
  $widget_ops = array(
   'classname' => 'microblog_widget',
   'description' => 'Allows you to display a list of microblog entries while excluding them from posts.',
  );
  $control_ops = array(
   'id_base' => 'microblog-widget',
  );
  $this->WP_Widget('microblog-widget', 'Microblog', $widget_ops, $control_ops );
 }
 function form ($instance) {
  $defaults = array(
   'numberposts' => '5',
   'title' => '',
   'rss' => '',
  );
  $instance = wp_parse_args( (array) $instance, $defaults );
?>
  <p>
    <label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
    <input type="text" name="<?php echo $this->get_field_name('title') ?>" id="<?php echo $this->get_field_id('title') ?> " value="<?php echo $instance['title'] ?>" size="20">
  </p>
  <p>
   <label for="<?php echo $this->get_field_id('numberposts'); ?>">Number of posts:</label>
   <input type="text" name="<?php echo $this->get_field_name('numberposts'); ?>" id="<?php echo $this->get_field_id('numberposts'); ?>" value="<?php echo $instance['numberposts']; ?>">
  </p>
  <p>
   <input type="checkbox" id="<?php echo $this->get_field_id('use_excerpt'); ?>" name="<?php echo $this->get_field_name('use_excerpt'); ?>" <?php if ($instance['use_excerpt']) echo 'checked="checked"' ?> />
   <label for="<?php echo $this->get_field_id('use_excerpt'); ?>">Show excerpts only?</label>
  </p>
  <p>
   <input type="checkbox" id="<?php echo $this->get_field_id('rss'); ?>" name="<?php echo $this->get_field_name('rss'); ?>" <?php if ($instance['rss']) echo 'checked="checked"' ?> />
   <label for="<?php echo $this->get_field_id('rss'); ?>">Show RSS feed link?</label>
  </p>
<?php
 }
 function update ($new_instance, $old_instance) {
  $instance = $old_instance;
  $instance['title'] = $new_instance['title'];
  $instance['numberposts'] = $new_instance['numberposts'];
  $instance['use_excerpt'] = $new_instance['use_excerpt'];
  $instance['rss'] = $new_instance['rss'];
  return $instance;
 }
 function widget ($args,$instance) {
  extract($args);
  $title = $instance['title'];
  $numberposts = $instance['numberposts'];
  $use_excerpt = $instance['use_excerpt'];
  $rss = $instance['rss'];
  // retrieve posts information from database
  global $post;
  $query = "post_type=micropost&posts_per_page=" . $numberposts;
  $query_results = new WP_Query($query);
  // build the widget contents!
  $out = "<ul>";
  while ( $query_results->have_posts() ) {
   $query_results->the_post();
   $out .= "<li>";
   $post_title = the_title( '', '', false );
   if ( $post_title ) {
    $out .= "<span class='microblog-widget-post-title'>"
          . $post_title
          . " </span>";
   }
   $out .= "<span class='microblog-widget-post-content'>";
   if ( $use_excerpt ) {
    add_filter('excerpt_more', 'micropost_excerpt_more');
    $out .= get_the_excerpt();
    remove_filter('excerpt_more', 'micropost_excerpt_more');
   } else {
    $out .= $post->post_content;
   }
   $out .= "</span>";
   $out .= "<span lass='microblog-widget-commentlink'>";
   $out .= " <a href='" . get_permalink() . "'>";
   $out .= "<img width='14px' src='"
         . site_url() . "/wp-includes/images/wlw/wp-comments.png'>";
   $out .= "&times;" . get_comments_number();
   $out .= '</a>';
   $out .= "</span>\n";
   $out .= "</li>\n";
  }
  $out .= "</ul>";
  //print the widget for the sidebar
  echo $before_widget;
  echo $before_title;
  echo $title;
  if ($rss) {
   echo ' <a href="' . get_site_url() . '/feed/?post_type=micropost" class="rss">';
   echo '<img src="' . site_url() . '/wp-includes/images/rss.png"/>';
   echo '</a>';
  }
  echo $after_title;
  echo $out;
  echo $after_widget;
  // clean up
  wp_reset_postdata();
 }
}
/**
     * Defines the visisble user name
     * @param object $user_info
     * @param string $wpam_user_name
     * @since 3.1
     */
 function get_username ($user_info, $wpam_user_name) {
        // Nick name option
        if ($wpam_user_name == 'nickname') {
            return $user_info->nickname;
        }
        // User login option
        if ($wpam_user_name == 'user_login') {
            return $user_info->user_login;
        }
        // Full name option (First name and last name)
        if ($wpam_user_name == 'full_name') {
            return $user_info->first_name . ' ' . $user_info->last_name;
        }
        // Default
        return $user_info->display_name;
    }
/*
 * Microblog shortcode code
*/
add_shortcode( 'microblog', 'microblog_shortcode' );
function microblog_shortcode($atts) {
 global $post;
 // 分页处理
 $paged = get_query_var('paged') ? get_query_var('paged') : 1;
 // Arguments to the shortcode
 extract( shortcode_atts( array(
  'num' => '3',
  'null_text' => '(none)',
  'show_date' => '',
  'date_format' => get_option('date_format'), // I recommend 'F j'
  'use_excerpt' => '',
  'q' => '',
 ), $atts ) );
 if ( $q ) {
  $q = str_replace ( "&#038;", "&", $q );
 }
 /*
 * query the database for tweets!
 * query syntax:
 * http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
 */
 $offset = ($paged-1)*$num;
 $query .= "post_type=micropost&posts_per_page=" . $num."&offset=".$offset;
 if ( $q ) {
  $query .= "&" . $q;
 }
 //echo $query;
 $query_results = new WP_Query($query);
 if ( $query_results->post_count == 0 ) {
  return "<p>" . wp_kses($null_text,array()) . "</p>\n";
 }
 $out = "<ul class='microblog-shortcode'>\n";
 while ( $query_results->have_posts() ) {
  $query_results->the_post();
  $avatar = get_avatar( get_the_author_email(), '24');;
  $out .= "<li> ".$avatar;
  if ( $show_date) {
   $out .= "<span class='microblog-shortcode-date'>"
         . get_the_date($date_format)
         . "</span>";
   $out .= "<span class='microblog-shortcode-date-sep'>: </span>\n";
  }
  $post_title = the_title( '', '', false );
  if ( $post_title ) {
   $out .= "<span class='microblog-shortcode-post-title'>"
         . $post_title
         . " </span></br>";
  }
  $out .= "<span class='microblog-shortcode-post-content'></br>";
  if ( $use_excerpt ) {
   add_filter('excerpt_more', 'micropost_excerpt_more');
   $out .= get_the_excerpt();
   remove_filter('excerpt_more', 'micropost_excerpt_more');
  } else {
   $out .= $post->post_content;
  }
  $out .= "</span></br>";
  $out .= "<span class='microblog-shortcode-commentlink'>评论:";
  $out .= " <a href='" . get_permalink() . "'>";
  $out .= "<img width='14px' src='"
        . site_url() . "/wp-includes/images/wlw/wp-comments.png'>";
  $out .= "&times;" . get_comments_number();
  $out .= "</a>";
  $out .= "</span>\n";
  $out .= "</li>\n";
  $out .= "<hr>\n";
 }
 $out .= "</ul>";
$args = array(
        'post_type' => 'micropost',
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' => 5,
        'post_status' => array('publish', 'pending', 'draft'),
        'paged' => $paged
    );
$the_query = new WP_Query($args);
 $total_pages = $the_query->max_num_pages;
 //echo $total_pages;
if ($total_pages > 1){
    $current_page = max(1, get_query_var('paged'));
 //echo $current_page;
    $out .= paginate_links(array(
        'base' => get_pagenum_link(1) . '%_%',
        'format' => '/page/%#%',
        'current' => $current_page,
        'total' => $total_pages,
        'prev_text' => __('« prev'),
        'next_text' => __('next »'),
    ));
}
 // clean up
 wp_reset_postdata();
 return $out;
}
function micropost_excerpt_more($more) {
 return ' ...';
}
/*
 * Load our default style sheet
*/
add_action( 'wp_print_styles', 'microblog_enqueue_styles' );
function microblog_enqueue_styles() {
 wp_register_style( 'simple-microblogging',
     plugins_url('simple-microblogging.css', __FILE__) );
 wp_enqueue_style( 'simple-microblogging' );
}
?>

修复分页将以下代码添加到主题的functions.php中:

function remove_page_from_query_string($query_string)
   { 
       if (isset($query_string['name']) && $query_string['name'] == 'page' && isset($query_string['page'])) {
           unset($query_string['name']);
           // 'page' in the query_string looks like '/2', so i'm spliting it out
           @list($delim, $page_index) = explode('/', $query_string['page']);
           $query_string['paged'] = $page_index;
       }      
       return $query_string;
   }
   // I will kill you if you remove this. I died two days for this line 
   add_filter('request', 'remove_page_from_query_string');

   // following are code adapted from Custom Post Type Category Pagination Fix by jdantzer
   function fix_category_pagination($qs){
       if(isset($qs['category_name']) && isset($qs['paged'])){
           $qs['post_type'] = get_post_types($args = array(
               'public' => true,
               '_builtin' => false
           ));
           array_push($qs['post_type'],'post');
       }
       return $qs;
   }
   add_filter('request', 'fix_category_pagination');

修改后的插件稍晚点提供下载链接,修复分页和页面样式。具体效果点击这里查看!

插件原作者链接:http://boolesrings.org

原始代码地址:https://github.com/boolesrings/Simple-microblogging-wordpress-plugin

☆文章版权声明☆

* 网站名称:obaby@mars
* 网址:https://h4ck.org.cn/
* 个性:https://oba.by/
* 本文标题: 《也谈WordPress说说/微博功能》
* 本文链接:https://h4ck.org.cn/2023/03/%e4%b9%9f%e8%b0%88wordpress%e8%af%b4%e8%af%b4-%e5%be%ae%e5%8d%9a%e5%8a%9f%e8%83%bd/
* 短链接:https://oba.by/?p=11659
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


分享文章:

猜你喜欢:

20 comments

    1. Google Chrome Google Chrome Mac Mac China山东省 移动 ip address 223.99.*.*

      嗯嗯,是可以嵌入。不过memos的发表评论是必须要注册的吧?这个就和wp的有点区别了。所以就想着直接用wp的体系来弄了

    1. Google Chrome Google Chrome Mac Mac China山东省 移动 ip address 223.99.*.*

      哈哈,你不早粗线呢,我还查了半天文档。不过wp的自定义类型文章分页确实恶心~~

  1. Safari Safari iPhone iPhone China四川省雅安市 电信 ip address 118.118.*.*

    大佬,厉害,晚上好,啥时候处理下为何我的手机浏览器从来都不能保存评论信息

    1. Google Chrome Google Chrome Windows Windows China山东省青岛市 联通 ip address 112.255.*.*

      勾选了保存用户信息以便下次评论吗?是不是浏览器防跟踪或者插件的问题?

    2. Google Chrome Google Chrome Windows Windows China山东省青岛市 联通 ip address 112.255.*.*

      你那里能看到我的blog的图片吗?我这里不显示了。百度云加速貌似挂了:https://su.baidu.com

      1. Mozilla Compatible Mozilla Compatible iPhone iPhone China四川省雅安市 电信 ip address 118.118.*.*

        我以为文章没有图!!!

            1. Google Chrome Google Chrome Android Android China山东省青岛市 联通 ip address 112.255.*.*

              是的 现在百度加速的主站可以访问了,我的还不行

  2. Google Chrome Google Chrome Mac Mac China河北省石家庄市 联通 ip address 101.24.*.*

    我2010年那会儿玩博客的时候用的wp程序,折腾了几年也没什么成果,去年年底开始折腾hexo,尤其是ChatGPTD 的出现,帮我我大忙,很多细枝末节的小问题都能解释,甚至可以编写命令行,报错也迅速分析给出解决方案,比去百度、谷歌搜索「某某报错解决方案」高效多了~

    1. Google Chrome Google Chrome Mac Mac China山东省 移动 ip address 223.99.*.*

      wp的用户群体不小,但是国内的开发氛围相对来说还是差一些,多数是技术文章来回拷贝。相反国外的高质量文章还是相对来说多一些,google搜索问题搜英文解决问题的概率能提高80%以上,不要搜中文,哈哈哈,搜中文就→成了国内的那些文章了,没啥价值。

  3. Google Chrome Google Chrome GNU/Linux GNU/Linux China辽宁省沈阳市 联通 ip address 60.16.*.*

    我现在都是在 QQ空间 吐槽 哈哈.

    本来想博客改 QQ空间那样的, 懒的折腾了 就放弃了.

    1. Google Chrome Google Chrome Mac Mac China山东省 移动 ip address 223.99.*.*

      嗯嗯。在这个地方吐槽的好处是可以自然屏蔽熟人。嘿嘿

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注