代码:
//添加碎语功能 添加到主题的functions.php文件中
function git_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,
'menu_icon' => 'dashicons-format-chat',
'hierarchical' => false,
'menu_position' => 4 ,
'supports' => array(
'editor',
'author',
'comments',
'title'
)
);
register_post_type('shuoshuo', $args);
}
add_action('init', 'git_shuoshuo');
//说说的固定连接格式
function custom_shuoshuo_link( $link, $post = 0 ){
if ( $post->post_type == 'shuoshuo' ){
return home_url( 'shuoshuo/' . $post->ID .'.html' );
} else {
return $link;
}
}
add_filter('post_type_link', 'custom_shuoshuo_link', 1, 3);
function custom_shuoshuo_rewrites_init(){
add_rewrite_rule(
'shuoshuo/([0-9]+)?.html$',
'index.php?post_type=shuoshuo&p=$matches[1]',
'top' );
}
add_action( 'init', 'custom_shuoshuo_rewrites_init' );
在主题的page目录或者主题根目录新建文件:shuoshuo.php 代码为下方代码。
<?php
/* template name: shuoshuo
description: template for xiaoxiaowu.me xiaoxiaowu theme
*/
get_header(); ?>
<style type="text/css">
.shuoshuo-content {
padding: 15px 30px 15px 30px;
min-height: 60px;
background-color: #fbfdfb;
position: relative;
box-shadow: 0 0 3px RGBA(0,0,0,.15);
color: #666;
letter-spacing: .0625rem;
margin-bottom: 20px;
font-size: 15px;
line-height: 30px;
}
.shuoshuo-meta {
margin-top: 20px;
text-align: right;
letter-spacing: 0;
}
@media screen and (min-width: 1000px) {
.mobileads, .shuoshuo-sjsj {
display: none !important
}
}
.zhutou {
position: relative;
top: -75px;
margin: 35px 0 0 15px;
float:left;
}
.shuoshuo li {
display: block;
padding: 8px 0;
}
</style>
<div class="pagewrapper clearfix">
<aside class="pagesidebar"> </aside>
<div class="pagecontent">
<header class="pageheader clearfix">
<h1 class="pull-left"></h1>
<div class="pull-right"><!--百度分享 --> <?php deel_share() ?></div>
</header>
<div class="shuoshuo">
<ul class="archives-monthlisting">
<li style="list-style-type: none;">
<ul class="archives-monthlisting"><?php $limit = get_option('posts_per_page'); $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts('post_type=shuoshuo&post_status=publish&showposts=' . $limit=6 . '&paged=' . $paged); if (have_posts()) : while (have_posts()) : the_post(); $x = wp_trim_words( get_the_content(),80); ?>
<li>
<div class="shuoshuo-content">
<?php echo $x; ?>
<div class="shuoshuo-meta"><i class="fa fa-user"></i> <?php the_author() ?> &nbsp (<?php the_time('Y年n月j日G:i'); ?>) &nbsp <a href="<?php the_permalink(); ?>" target="_blank" rel="noopener noreferrer" data-original-title="查看详情">查看详情 | 评论</a></div>
</div></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
有时候想在博客发发牢骚,有时候看到一些喜欢的文章,但又和博客的内容方向不一样,这时候就需要动动手改博客了。之前一直都是新建了一个名为博主随笔的分类目录,然后把说说和喜欢的文章发布在那,可是这样很影响博客呢,所以这2天给博客弄了个发布说说的功能。 代码已经贴在上方,部分地方需要自行根据你的wordpress主题来修改,自己弄的才是最适合自己的,以上代码只是分享出来给有同样想法的童鞋一个参考。
