纯代码给wordpress增加说说/微博/微语功能

作为一个独立的个人博客,说说怎么少得了,有时候我们写日志,或者做通告,长不长,短不短的话语总是纠结怎么写成文章,但是为了人性化,像qq空间 的说说,emlog的微语以及微博,都是把短语发挥到了极致,无奈wordpress没有这项功能,今天我就教大家添加该功能,非常简单,代码也很少。从 此踏上说说的征途吧~

首先在主题的functions.php里面加入以下代码:

  • //说说

  • add_action(‘init’, ‘my_custom_init’); function my_custom_init() { $labels = array( ‘name’ => ‘说说’, ‘singular_name’ => ‘singularname’, ‘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’) ); register_post_type(‘shuoshuo’,$args); }

之后创建一个php文档,把以下内容复制进去:

  • <?php /*

  • Template Name: 说说

  • author: 秋叶

  • url: http://www.wuover.com/141.html

  • */

  • get_header(); ?><div class=”shuoshuo”>

  • <ul class=”archives-monthlisting”>

  • <?php query_posts(“post_type=shuoshuo&post_status=publish&posts_per_page=-1”);if (have_posts()) : while (have_posts()) : the_post(); ?>

  • <li><span class=”tt”><?php the_time(‘Y年n月j日G:H’); ?></span>

  • <div class=”shuoshuo-content”><?php the_content(); ?><br/><div class=”shuoshuo-meta”><span >—<?php the_author() ?></span></div></div><?php endwhile;endif; ?></li>

  • </ul>

  • </div>

  • <?php get_footer();?>

保存该文档,命名为shuoshuo.php,将其放于主题目录下。

接下来就是进入wordpress后台新建页面,标题写“我的说说”,模版选择“说说”,发布,大功告成,之后可通过后台发表说说了。以上不提供样式,请各位自行设置样式,充分发挥自己的想象力。效果请看:http://www.wuover.com/shuo

说说

注:如果不想富媒体说说,单纯地写文字的话,可以把最后一段代码中的<?php the_content(); ?>改成<?php the_title(); ?>,这样以后发表说说只要填写标题就可以了,查找起来也比较方便。如果你用<?php the_content(); ?>,那么你发表说说的时候标题和内容要写成一样,以方便查找,如果你只填写内容,那么你在后台查看说说的时候,显示的列表全是无标题,对于修改比 较麻烦。

如遇代码错误请批量转换全角符号至半角符号。至于分页问题很多人问道,请看这篇文章《解决WordPress自定义页面分页问题

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *