最近看别人博客受到点启发,于是自己也做了个说说的功能,当然,功能还比较简陋,以后大概会完善一下。(咕咕咕)

这个功能的叫法比较多,有叫微博的有叫微语的有叫说说的,反正都是一种东西了。附上插件下载:

百度网盘 yxfd

插件会在后台新建一个文章类别“说说”,同时在小工具中会新建一个“说说小工具”,目前的说说还不支持评论和点赞的功能,也不支持前台发布。

注意,这个css我只是随便写了一下,如果需要用到自己博客的话可能需要改改。=_=

小工具:

页面:


插件代码:

<?
/*
	Plugin Name: 说说
	Plugin URI: https://www.yeraph.com
	Description: 说说
	Author: Yeraph
	Author URI: https://www.yeraph.com
    Version: 1.0.0

*/

if (!defined('ABSPATH')) die();
if (!defined('shuoshuo_DIR'))     define('shuoshuo_DIR',  plugin_dir_path(__FILE__));


function addmycss(){
	echo "<link rel='stylesheet' href='".home_url('wp-content/plugins/shuoshuo/style.css')."' type='text/css' />\n";
}
add_action('wp_head','addmycss');


//创建一个新文章类别
add_action('init', 'lx_shuoshuo');   
function lx_shuoshuo()    
{   
  $labels = array(   
    'name' => '说说',   
    'singular_name' => '说说',   
    'add_new' => '发布说说',   
    'add_new_item' => '发布说说',   
    'edit_item' => '编辑说说',   
    'new_item' => '新说说',   
    'view_item' => '查看说说',   
    'search_items' => '搜索说说',   
    'not_found' =>  '没有内容',   
    'not_found_in_trash' => '空',    
    'parent_item_colon' => '',   
    'menu_name' => '说说'   
  
  );   
  $args = array(   
    'labels' => $labels,   
    'public' => true,   
    'publicly_queryable' => true,   
    'show_ui' => true,    
    'show_in_menu' => true,    
    'query_var' => true,   
    'rewrite' => true,   
    'capability_type' => 'post',   
    'has_archive' => true,    
    'hierarchical' => false,   
    'menu_position' => null,   
    'supports' => array('title','editor','author','thumbnail','excerpt','comments','custom-fields')   
  );    
  register_post_type('wp_shuoshuo',$args);   
}  

//创建一个小工具
wp_register_sidebar_widget(
    'wp_shuoshuo',        
    '说说小工具',          
    'shuoshuo_widget'  
);

//小工具函数
function shuoshuo_widget() {
    $args = array( 
      'post_type'=>'wp_shuoshuo',
      'post_status'=>'publish',
      'posts_per_page'=>10,
      'paged'=>1,
      'orderby'=>'date',
      'order'=>'DESC'
    ); 
    $my_query = new WP_Query($args);
	
	echo '<div class="shuo-widget"><div class="widget-title"><span>随便说说</span></div>';
	
    if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post();
        ?>
<ul class="widget-hotpost-brief">
	<li><h4><?php the_title(); ?></h4>
	<div>
		<?php echo strip_tags(get_the_content()); ?>
	</div>
	<div class="widget-hotpost-brief-time">
		<span class="time" ><?php echo timeago(get_gmt_from_date(get_the_date('Y-m-d G:i:s'))); ?></span>
	</div>
	</li>

</ul>
<?php 
	endwhile; 
		wp_reset_query(); 
    }
	echo '<a class="shuo-widget-btn" href="/shuoshuo" target="_blank">浏览更多</a></div>';

}

//引入说说页面并重写url
add_action('generate_rewrite_rules', 'shuoshuo_rewrite_rules' );
function shuoshuo_rewrite_rules( $wp_rewrite ){   
    $new_rules = array(    
        'shuoshuo/?$' => 'index.php?shuoshuo_page=shuoshuo',   
    ); 
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;   
}

add_action('query_vars', 'shuoshuo_add_query_vars');   
function shuoshuo_add_query_vars($public_query_vars){     
    $public_query_vars[] = 'shuoshuo_page';
       
    return $public_query_vars;     
}

add_action("template_redirect", 'shuoshuo_template_redirect');   
function shuoshuo_template_redirect(){   
    global $wp;   
    global $wp_query, $wp_rewrite;
    $reditect_page =  isset($wp_query->query_vars['shuoshuo_page']) ? $wp_query->query_vars['shuoshuo_page'] : '';   
    if ($reditect_page == "shuoshuo"){   
        include(shuoshuo_DIR.'/shuoshuo_page.php');   
         die();  
    }   
} 

register_activation_hook(__FILE__,'shuoshuo_flush_rewrite_rules' );   
function shuoshuo_flush_rewrite_rules() { 
	    global $wp_rewrite; 
		$wp_rewrite->flush_rules();   
}

add_filter('redirect_canonical', 'shuoshuo_redirect_canonical');
function shuoshuo_redirect_canonical($redirect_url){
    if( get_query_var('shuoshuo_page')) return false;
    return $redirect_url;
}

?>

说说页面(shuoshuo_page.php):

<?php

get_header(); ?>

<?php 

//创建分页参数
$page=1;  
if(isset($_GET['page'])&&!empty($_GET['page'])){
		$page=intval($_GET['page']);
}                  
                  
                  
$args = array( 
      'post_type'=>'wp_shuoshuo',
      'post_status'=>'publish',
      'posts_per_page'=>10,
      'paged'=>$page,
      'orderby'=>'date',
      'order'=>'DESC'
    ); 
    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post();
        ?>


		
	<div class="shuo-card">
		<h4 class="title"><?php the_title(); ?></h4>
		<?php echo strip_tags(get_the_content()); ?>
		<div class="time">
			<span title="<?php the_date('Y-m-d H:i'); ?>"><?php echo timeago(get_gmt_from_date(get_the_date('Y-m-d G:i:s'))); ?></span>
		</div>
	</div>
<?php
			endwhile;
			wp_reset_query(); //重置 query 查询
       }
                  ?>
                  
<?php

$postcount = wp_count_posts('wp_shuoshuo');
$postcount = $postcount->publish;
$pagea=ceil($postcount/10);
if($page !== 1){
   echo '<a class="button" href="'.home_url("/kx?page=".($page-1)).'" >上一页</a>';
}
if($page<$pagea){
	echo '<a class="button" href="'.home_url("/kx?page=".($page+1)).'" >下一页</a>';
}

?>                      

		<?php
			// If comments are open or we have at least one comment, load up the comment template.
			if ( comments_open() || get_comments_number() ) :
				comments_template();
			endif;
		?>

<?php
get_footer(); ?>

样式文件(style.css),注意,这个css我只是随便写了一下,如果需要用到自己博客的话可能需要改改。=_=

.shuo-widget {
    background: #fff;
    margin-bottom: 30px;
    padding: 25px;
	position: relative;
}
.shuo-widget .widget-title {
    margin-bottom: 25px;
    font-size: 15px;
    font-weight: 600;
    padding-bottom: 5px;
    border-bottom: 1px solid #ddd;
    position: relative;
}
.shuo-widget .widget-title span {
    display: inline-block;
    padding-right: 20px;
    position: relative;
}
.shuo-widget .widget-title span:after {
    position: absolute;
    z-index: 2;
    content: "";
    border-bottom: 1px solid #000;
    width: 100%;
    height: 1px;
    left: 0;
    bottom: -6px;
}
.shuo-widget ul {
    padding: 0;
    margin: 0;
    list-style: none;
}
.widget-hotpost-brief li {
    position: relative;
    padding-left: 0px;
    margin-bottom: 12px;
    list-style: square inside;
}
.widget-hotpost-brief li .widget-hotpost-brief-time {
    color: #bbb;
}
.shuo-widget-btn {
    display: inline-block;
    padding: 6px 16px;
    text-align: center;
    border-radius: 2px;
    color: #fff !important;
    background: #171717;
    border: 0px !important;
}
.shuo-card {
	background: #fff;
    padding: 45px 55px;
    margin-bottom: 40px;
}
.shuo-card .title {
    font-size: 26px;
    font-weight: normal;
    margin-bottom: .8em;
    display: inline-block;
    position: relative;
}
.shuo-card .time {
    padding-top: 1.5em;
}
.shuo-card span {
    padding: 0 .5em;
    color: #aaa;
    font-size: 13px;
}