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

avatar 2017年02月02日20:38:34 20 13771 views
博主分享免费Java教学视频,B站账号:Java刘哥
作为一个独立的个人博客,说说怎么少得了,有时候我们写日志,或者做通告,长不长,短不短的话语总是纠结怎么写成文章,但是为了人性化,像qq空间的说说,emlog的微语以及微博,都是把短语发挥到了极致,无奈wordpress没有这项功能,今天我就教大家添加该功能,非常简单,代码也很少。从此踏上说说的征途吧~ 首先在主题的functions.php里面加入以下代码:
  1. //说说
  2.     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文档,把以下内容(顺便附上css样式)复制进去:
  1. <?php /*
  2.     Template Name: 说说
  3.     author: 秋叶
  4.     url: http://www.mizuiren.com/141.html
  5.     */
  6.     get_header(); ?>
  7. <style type="text/css">
  8.     #shuoshuo_content {
  9.         background-color: #fff;
  10.         padding: 10px;
  11.         min-height: 500px;
  12.     }
  13.     /* shuo */
  14.     body.theme-dark .cbp_tmtimeline::before {
  15.         background: RGBA(255, 255, 255, 0.06);
  16.     }
  17.     ul.cbp_tmtimeline {
  18.         padding: 0;
  19.     }
  20.     div class.cdp_tmlabel > li .cbp_tmlabel {
  21.         margin-bottom: 0;
  22.     }
  23.     .cbp_tmtimeline {
  24.         margin: 30px 0 0 0;
  25.         padding: 0;
  26.         list-style: none;
  27.         position: relative;
  28.     }
  29.     /* The line */
  30.     .cbp_tmtimeline:before {
  31.         content: '';
  32.         position: absolute;
  33.         top: 0;
  34.         bottom: 0;
  35.         width: 4px;
  36.         background: RGBA(0, 0, 0, 0.02);
  37.         left: 80px;
  38.         margin-left: 10px;
  39.     }
  40.     /* The date/time */
  41.     .cbp_tmtimeline > li .cbp_tmtime {
  42.         display: block;
  43.         /* width: 29%; */
  44.         /* padding-right: 110px; */
  45.         max-width: 70px;
  46.         position: absolute;
  47.     }
  48.     .cbp_tmtimeline > li .cbp_tmtime span {
  49.         display: block;
  50.         text-align: right;
  51.     }
  52.     .cbp_tmtimeline > li .cbp_tmtime span:first-child {
  53.         font-size: 0.9em;
  54.         color: #bdd0db;
  55.     }
  56.     .cbp_tmtimeline > li .cbp_tmtime span:last-child {
  57.         font-size: 1.2em;
  58.         color: #9BCD9B;
  59.     }
  60.     .cbp_tmtimeline > li:nth-child(odd) .cbp_tmtime span:last-child {
  61.         color: RGBA(255, 125, 73, 0.75);
  62.     }
  63.     div.cbp_tmlabel > p {
  64.         margin-bottom: 0;
  65.     }
  66.     /* Right content */
  67.     .cbp_tmtimeline > li .cbp_tmlabel {
  68.         margin: 0 0 45px 65px;
  69.         background: #9BCD9B;
  70.         color: #fff;
  71.         padding: .8em 1.2em .4em 1.2em;
  72.         /* font-size: 1.2em; */
  73.         font-weight: 300;
  74.         line-height: 1.4;
  75.         position: relative;
  76.         border-radius: 5px;
  77.         transition: all 0.3s ease 0s;
  78.         box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  79.         cursor: pointer;
  80.         display: block;
  81.     }
  82.     .cbp_tmlabel:hover {
  83.         /* transform:scale(1.05); */
  84.         transform: translateY(-3px);
  85.         z-index: 1;
  86.         -webkit-box-shadow: 0 15px 32px rgba(0, 0, 0, 0.15) !important
  87.     }
  88.     .cbp_tmtimeline > li:nth-child(odd) .cbp_tmlabel {
  89.         background: RGBA(255, 125, 73, 0.75);
  90.     }
  91.     /* The triangle */
  92.     .cbp_tmtimeline > li .cbp_tmlabel:after {
  93.         right: 100%;
  94.         border: solid transparent;
  95.         content: " ";
  96.         height: 0;
  97.         width: 0;
  98.         position: absolute;
  99.         pointer-events: none;
  100.         border-right-color: #9BCD9B;
  101.         border-width: 10px;
  102.         top: 4px;
  103.     }
  104.     .cbp_tmtimeline > li:nth-child(odd) .cbp_tmlabel:after {
  105.         border-right-color: RGBA(255, 125, 73, 0.75);
  106.     }
  107.     p.shuoshuo_time {
  108.         margin-top: 10px;
  109.         border-top: 1px dashed #fff;
  110.         padding-top: 5px;
  111.     }
  112.     /* Media */
  113.     @media screen and (max-width: 65.375em) {
  114.         .cbp_tmtimeline > li .cbp_tmtime span:last-child {
  115.             font-size: 1.2em;
  116.         }
  117.     }
  118.     .shuoshuo_author_img img {
  119.         border: 1px solid #ddd;
  120.         padding: 2px;
  121.         float: left;
  122.         border-radius: 64px;
  123.         transition: all 1.0s;
  124.     }
  125.     .avatar {
  126.         -webkit-border-radius: 100% !important;
  127.         -moz-border-radius: 100% !important;
  128.         box-shadow: inset 0 -1px 0 #3333sf;
  129.         -webkit-box-shadow: inset 0 -1px 0 #3333sf;
  130.         -webkit-transition: 0.4s;
  131.         -webkit-transition: -webkit-transform 0.4s ease-out;
  132.         transition: transform 0.4s ease-out;
  133.         -moz-transition: -moz-transform 0.4s ease-out;
  134.     }
  135.     .zhuan {
  136.         transform: rotateZ(720deg);
  137.         -webkit-transform: rotateZ(720deg);
  138.         -moz-transform: rotateZ(720deg);
  139.     }
  140.     /* end */
  141. </style>
  142. </head>
  143. <body>
  144. <div id="primary" class="content-area" style="">
  145.     <main id="main" class="site-main" role="main">
  146.         <div id="shuoshuo_content">
  147.             <ul class="cbp_tmtimeline">
  148.                 <?php query_posts("post_type=shuoshuo&post_status=publish&posts_per_page=-1");if (have_posts()) : while (have_posts()) : the_post(); ?>
  149.                 <li> <span class="shuoshuo_author_img"><img src="https://oss.liuyanzhao.com/uploads/2017/02/zhitianjiahui-e1489667385398.png" class="avatar avatar-48" width="48" height="48"></span>
  150.                     <a class="cbp_tmlabel" href="javascript:void(0)">
  151.                         <p></p>
  152.                         <p><?php the_content(); ?></p>
  153.                         <p></p>
  154.                         <p class="shuoshuo_time"><i class="fa fa-clock-o"></i>
  155.                             <?php the_time('Y年n月j日G:i'); ?>
  156.                         </p>
  157.                     </a>
  158.                     <?php endwhile;endif; ?>
  159.                 </li>
  160.             </ul>
  161.         </div>
  162.     </main>
  163.     <!-- .site-main -->
  164. </div>
  165. <script type="text/javascript">
  166.     $(function () {
  167.         var oldClass = "";
  168.         var Obj = "";
  169.         $(".cbp_tmtimeline li").hover(function () {
  170.             Obj = $(this).children(".shuoshuo_author_img");
  171.             Obj = Obj.children("img");
  172.             oldClass = Obj.attr("class");
  173.             var newClass = oldClass + " zhuan";
  174.             Obj.attr("class", newClass);
  175.         }, function () {
  176.             Obj.attr("class", oldClass);
  177.         })
  178.     })
  179. </script>
  180. <?php get_sidebar(); ?>
  181. <?php get_footer();?>
  保存该文档,命名为shuoshuo.php,将其放于主题目录下(记得是主题目录下)。shuoshuo.php里可以修改头像。 接下来就是进入wordpress后台新建页面,标题写“我的说说”,模版选择“说说”,发布,大功告成,之后可通过后台发表说说了。以上不提供样式,请各位自行设置样式,充分发挥自己的想象力。效果请看 http://liuyanzhao.com/shuoshuo.html 注:如果不想富媒体说说,单纯地写文字的话,可以把最后一段代码中的<?php the_content(); ?>改成<?php the_title(); ?>,这样以后发表说说只要填写标题就可以了,查找起来也比较方便。如果你用<?php the_content(); ?>,那么你发表说说的时候标题和内容要写成一样,以方便查找,如果你只填写内容,那么你在后台查看说说的时候,显示的列表全是无标题,对于修改比较麻烦。 如遇代码错误请批量转换全角符号至半角符号。至于分页问题很多人问道,请看这篇文章《解决WordPress自定义页面分页问题》  
说说效果图:http://liuyanzhao.com/shuoshuo.html css代码取自于 憧憬博客
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:12   待审核评论数:0
  1. avatar 文摘笔记

    通过教程成功的实现了说说功能,效果很不错,非常感谢,但是发表说说的时候如果里面带有文章链接的话就会错行显示异常不知道是哪里的原因,有没有办法可以解决呢?

  2. avatar

    问一下怎么实现分页

  3. avatar rmrf97

    博主为啥我像这样添加然后就在网站的最上面提示乱码了 可以帮我解答一下嘛

  4. avatar 涂宗勋

    很强大,试了一下,没想到真的可以了,感谢!

  5. avatar ToString

    博主 php文件 js有一处报错

  6. avatar wordpress小白

    说说可以支持评论功能吗

  7. avatar 云笑

    博主你好 我想问一下,可不可以把 说说样式那个白底弄成透明的?http://yunx.08tk.cn/wp-content/uploads/2017/06/QQ截图20170604205248.png 该怎么改? 我是小白 不知道这么说博主能不能明白 :?:

    • avatar 言曌

      可以的,像这样吗?https://liuyanzhao.com/wp-content/uploads/2017/06/QQ截图20170604223001.jpg 你可以刷新一下,看下

      • avatar 云笑

        对的 就是这样 我自己糊弄了好久都没弄好 不知道博主怎么实现的 能不能分享下教程

  8. avatar 狂放

    在网上找了半天,没想到在这找到了

    • avatar 言曌

      没事多来看看

      • avatar 狂放

        你这评论者UA显示把我的安卓版本,手机型号都显示出来了 :smile:

  9. avatar 壹号特工

    转走了,感谢博主

  10. avatar whyzaa

    已添加,感觉挺漂亮的 .