WordPress添加“说说”页面

  根据原作者的作品做了几处细节修改,修改的地方放到最后,先上源码:

第一步:引入功能代码

  添加以下代码到”主题根目录“下的functions.php文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//说说
add_action('init', 'my_custom_init'); function my_custom_init() { $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','custom-fields','comments') ); register_post_type('say、',$args); }
//指定说说文章模板
add_filter( 'template_include', 'include_template_function', 1 );
function include_template_function( $template_path ) {
if ( get_post_type() == 'say' ) {
if ( is_single() ) {
if ( $theme_file = locate_template( array ( 'says.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . '/pages/says.php';//自己修改文件路径
}
}
}
return $template_path;
}
/*说说点赞功能*/
add_action('wp_ajax_nopriv_bigfa_like', 'bigfa_like');
add_action('wp_ajax_bigfa_like', 'bigfa_like');
function bigfa_like(){
global $wpdb,$post;
$id = $_POST["um_id"];
$action = $_POST["um_action"];
if ( $action == 'ding'){
$bigfa_raters = get_post_meta($id,'bigfa_ding',true);
$expire = time() + 99999999;
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhost
setcookie('bigfa_ding_'.$id,$id,$expire,'/',$domain,false);
if (!$bigfa_raters || !is_numeric($bigfa_raters)) {
update_post_meta($id, 'bigfa_ding', 1);
}
else {
update_post_meta($id, 'bigfa_ding', ($bigfa_raters + 1));
}
echo get_post_meta($id,'bigfa_ding',true);
}
die;
}

第二步:创建说说页面

  进入“主题根目录—-pages目录”创建says.php(名称一定不要错)文件,加入以下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php

/**
* Template name: YXC-说说页面
* Description: YXC-say
*/

// 获取链接列表
get_header();
$header_style = zib_get_page_header_style();
?>
<main class="container">
<div class="content-wrap">
<div class="content-layout">
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php if ($header_style != 1) {
echo zib_get_page_header();
} ?>
<!--说说主体开始blog.catchwang.com-->
<style type="text/css">
#article{color:#444;font-family:microsoft yahei}
#article ul{padding-left:0!important}
#article li{padding:5px!important;color:var(--main-color)}
#article img{border-radius:5px}
#article p{color:var(--main-color)}
#article video{border-radius:5px}
.typebox{box-shadow:0 .125rem .25rem rgb(0 0 0 / 8%);padding:10px;margin-bottom:20px;border-radius:8px}
.yxc-avatar img{border-radius:99px;width:25px;height:25px}
.yxc-hr{border-top:2px dashed rgba(255,78,106,.85);margin:0 0 0}
.yxc-content{positiON:relative}
.yxc-content .say-icon{position:absolute;right:1px;display:block;width:40px;height:40px;line-height:20px;background-image:url(https://cdn.catchwang.com/wp-resources/love.svg);transform-origin:0 0}
#yxc-content{background-color:rgba(255,78,106,.15);color:#ff4e6a;padding:2px 5px;display:inline-block}
#yxc-content1{background-color:rgb(41,151,247,.15);color:#2997f7;padding:2px 5px;display:inline-block}
.yxc-fenye{box-shadow:0 1px 6px .01px #9999993d;border-radius:999px}
.yxc-h2 h2{display:block;font-size:1.5em;margin-block-start:.83em;margin-block-end:.83em;margin-inline-start:0;margin-inline-end:0;font-weight:700;margin:0}
.yxc-h2 h2::before{content:"# ";color:#f04494;font-weight:700}
.button{padding:5px 25px;font-size:16px;margin:4px 2px;-webkit-transition-duration:.4s;transition-duration:.4s;cursor:pointer;border-radius:10px!important}
.button1{background-color:var(--main-bg-color);color:#2997f7;border:2px solid #2997f7}
.button1:hover{background-color:#2997f7;color:#fff}
.aligncenter{clear:both;display:block;margin:0 auto}
.alignright{display:block;margin-left:auto}
.yxc_frame{padding:0 10px 0 10px}
.post-like{text-align:right;padding:10px}
.post-like a{border-radius:10px;color:#2997f7;font-size:15px;padding:8px 10px;text-decoration:none;outline:0}
.post-like a.done,.post-like a:hover{color:#ff4e6a}
.post-like a.done{cursor:not-allowed}
.wp-posts-content img{max-width:30%!important}
#yxc-bt>p{display:flex!important;flex-wrap:wrap;flex-direction:column;align-content:space-between;align-items:flex-start;justify-content:center}
.yxc-fenye,.yxc_bj{background:var(--main-bg-color)}
.ty-tyle{display:flex;flex-wrap:nowrap;align-content:center;align-items:center}
.ty-tyle *{display:inline-block}
.typeafter{display:flex;justify-content:space-between}
.yxc-con-ty{margin-left:5px;border-radius:99px}
</style>
<!--点赞功能js-->
<script type="text/javascript">
$.fn.postLike=function(){if($(this).hasClass('done')){return false}else{$(this).addClass('done');var id=$(this).data("id"),action=$(this).data('action'),rateHolder=$(this).children('.count');var ajax_data={action:"bigfa_like",um_id:id,um_action:action};$.post("/wp-admin/admin-ajax.php",ajax_data,function(data){$(rateHolder).html(data)});return false}};$(document).on("click",".favorite",function(){$(this).postLike()});
</script>
</head>
<body>
<h1 class="wp-block-zibllblock-biaoti title-theme"><strong>说说列表:</strong></h1>
<h1 class="wp-block-zibllblock-biaoti title-theme"><strong>目前总计发表 <span style="color: #ff0000;"><?php $count_posts = wp_count_posts('say'); echo $published_posts = $count_posts->publish;?></span> 个说说,更新于 <span style="color: #ff0000;"><?php $last = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'say')");$last = date('Ynj日', strtotime($last[0]->MAX_m));echo $last; ?></span>。<strong></h1>
</br>
<div class="yxc-content">
<ul id="list">
<?php $limit = get_option('posts_per_page');$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('post_type=say&post_status=publish&showposts=' . $limit=6 . '&paged=' . $paged);if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<div class="typebox yxc_bj">
<a class="say-icon"></a>


<div class="yxc_frame">
<div class="yxc-h2">
<h2><?php the_title();?></h2>
</div>
<div id="article" class="wp-posts-content" style="margin: 18px;">
<div id="yxc-bt">
<?php the_content(); ?>
</div>
</div>
</div>
<hr class="yxc-hr" >
<!--点赞功能开始-->
<div class="typeafter">
<div class=" ty-tyle">
<div class="yxc-avatar">
<a href="<?php bloginfo('url'); ?>"><?php echo get_avatar( get_the_author_meta( 'ID' ) ); ?></a>
</div>
<a href="<?php the_author_url(); ?>"><span class="yxc-con-ty" id="yxc-content"><i class="fa fa-user" aria-hidden="true"></i> <?php the_author(); ?>の说说</span></a>
<span id="yxc-content1" class="yxc-con-ty"><i class="fa fa-calendar" aria-hidden="true"></i> 发布于<?php the_time('YnjG:i'); ?></span>
</div>
<div class="post-like">
<a href="javascript:;" data-action="ding" data-id="<?php the_ID(); ?>" class="favorite<?php if(isset($_COOKIE['bigfa_ding_'.$post->ID])) echo ' done';?>"><i class="fa fa-heart" aria-hidden="true"></i> 喜欢[<span class="count"><?php if( get_post_meta($post->ID,'bigfa_ding',true)){ echo get_post_meta($post->ID,'bigfa_ding',true);} else { echo '0';}?></span>]
</a>
</div>
</div>

</div>
<!---->
<?php endwhile;endif; ?>
</li>
</ul>
<!--分页功能-->
<div class="yxc-fenye" style="text-align:center;font-weight:700;border-radius: 8px;">
<?php if (function_exists('wp_pagenavi')) wp_pagenavi();else { ?><div style="padding: 5px;margin: 30px;"><?php previous_posts_link('<button class="button button1"><i class="fa fa-caret-left" aria-hidden="true"></i> 上一页</button>') ?> <?php next_posts_link('<button class="button button1">下一页 <i class="fa fa-caret-right" aria-hidden="true"></i></button>') ?></div><?php } ?>
</div>
</div>
<!--说说主体结束blog.catchwang.com-->
</div>
<!---->
<!---->
<?php comments_template('/template/comments.php', true); ?>
</div>
</div>
<?php get_sidebar(); ?>
</main>
<?php
get_footer();

  到此说说界面就完成了,是不是很简单呢?

细节修改:

  1. 换页按钮外框距底部太近——修改为距离30px
  2. 换页按钮外框圆角太大,与说说列表不和谐——修改为8px
    1
  3. 说说内容与标题距离太近——修改为20px,且文章内容与标题对其
    2
  4. 说说内文章英文名类型——修改名称为说说,与文章/帖子类型相似,更和谐
    3
    此文取自星语的小木屋,文章有较大的幅度修改