-
Notifications
You must be signed in to change notification settings - Fork 14
/
functions.php
648 lines (603 loc) · 26.2 KB
/
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<?php
require_once file_exists(get_stylesheet_directory() . '/lib/core.php') ? get_stylesheet_directory() . '/lib/core.php' : get_template_directory() . '/lib/core.php';
//主题检查更新模块
require_once(TEMPLATEPATH . '/lib/theme-update-checker.php');
$wpdaxue_update_checker = new ThemeUpdateChecker(
'DChaser',
'http://dchaser.sinaapp.com/update/dchaser-update.json'
);
if (!isset($content_width)) $content_width = gk_config('content_width');
add_action('after_setup_theme', 'gk_setup');
if (!function_exists('gk_setup')) {
function gk_setup() {
//支持语言包
load_theme_textdomain('gkwp', __PROOT__ . '/languages');
$languages = gk_config('languages');
foreach ($languages as $name => $path) {
load_theme_textdomain($name, $path);
}
//设置主题支持
$theme_supports = gk_config('theme_support');
foreach ($theme_supports as $key => $support) {
is_numeric($key) ? add_theme_support($support) : add_theme_support($key, $support);
}
register_nav_menus(gk_config('register_nav_menus'));
//设置缩略图
$thumbnails = gk_config('thumbnails');
foreach ($thumbnails as $name => $setting) {
set_post_thumbnail_size($setting[0], $setting[1], $setting[2]);
add_image_size($name, $setting[0], $setting[1], $setting[2]);
}
//支持 register_default_headers
$register_default_headers_config = gk_config('register_default_headers');
if (!empty($register_default_headers_config)) register_default_headers($register_default_headers_config);
require_once get_gk_file('lib/walker.php');
}
}
//注册侧栏
add_action('widgets_init', 'gk_widgets_init');
if (!function_exists('gk_widgets_init')) {
function gk_widgets_init() {
//注册系统widgets
require_once get_gk_file('lib/widgets.php');
//替换默认的widget
$widgets=array('Widget_Recent_Comments','Widget_Recent_Comments','Widget_Pages','Widget_Links','Widget_Search','Widget_Archives','Widget_Meta','Widget_Text','Widget_Categories','Widget_Recent_Posts','Widget_RSS','Widget_Tag_Cloud','Nav_Menu_Widget','Nav_Menu_Widget','Widget_Calendar');
foreach($widgets as $widget){
unregister_widget('WP_'.$widget);
register_widget('GK_'.$widget);
}
register_widget('GK_Widget_weiboShow');
//注册边栏
$sidebars = gk_config('sidebar');
foreach ($sidebars as $sidebar) {
unset($sidebar['default']);
register_sidebar($sidebar);
}
if(gk_config('sidebar_debug')){
//边栏时时调试
gk_set_sidebar_default();
}
}
}
//菜单默认显示首页
add_filter('wp_page_menu_args', 'gk_page_menu_args');
if (!function_exists('gk_page_menu_args')) {
function gk_page_menu_args($args) {
$args['show_home'] = true;
return $args;
}
}
//选择主题后初始化
add_action('after_switch_theme', 'gk_set_sidebar_default');
if (!function_exists('gk_set_sidebar_default')) {
function gk_set_sidebar_default() {
global $_wp_sidebars_widgets;
//初始化widget
$widgets = gk_config('widget');
foreach ($widgets as $name => $setting) {
$id = 102;
$option = get_option('widget_'.$name);
if (!$option) $option = array();
if (!isset($option[$id]) || gk_config('sidebar_debug')) {
$option[$id] = $setting;
update_option('widget_'.$name, $option);
}
}
//初始化边栏
$sidebars_option = wp_get_sidebars_widgets();
$update_sidebar = true;
$sidebars = gk_config('sidebar');
foreach ($sidebars as $sidebar) {
if (isset($sidebar['default']) && (!isset($sidebars_option[$sidebar['id']]) || gk_config('sidebar_debug')) ) {
$sidebars_option[$sidebar['id']] = array_map(create_function('$v', 'return $v.\'-102\';'),explode(',', $sidebar['default']));
$update_sidebar = true;
}
}
if ($update_sidebar) {
$_wp_sidebars_widgets = $sidebars_option;
wp_set_sidebars_widgets($sidebars_option);
}
}
}
//获取日志归档
function archives_list_SHe() {
global $wpdb,$month;
$lastpost = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_date <'" . current_time('mysql') . "' AND post_status='publish' AND post_type='post' AND post_password='' ORDER BY post_date DESC LIMIT 1");
$output = get_option('SHe_archives_'.$lastpost);
if(empty($output)){
$output = '';
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'SHe_archives_%'");
$q = "SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM $wpdb->posts p WHERE post_date <'" . current_time('mysql') . "' AND post_status='publish' AND post_type='post' AND post_password='' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";
$monthresults = $wpdb->get_results($q);
if ($monthresults) {
foreach ($monthresults as $monthresult) {
$thismonth = zeroise($monthresult->month, 2);
$thisyear = $monthresult->year;
$q = "SELECT ID, post_date, post_title, comment_count FROM $wpdb->posts p WHERE post_date LIKE '$thisyear-$thismonth-%' AND post_date AND post_status='publish' AND post_type='post' AND post_password='' ORDER BY post_date DESC";
$postresults = $wpdb->get_results($q);
if ($postresults) {
$text = sprintf('%s %d', $month[zeroise($monthresult->month,2)], $monthresult->year);
$postcount = count($postresults);
$output .= '<ul class="archives-list"><li><span class="archives-yearmonth">' . $text . ' (' . count($postresults) . ' 篇文章)</span><ul class="archives-monthlisting">' . "\n";
foreach ($postresults as $postresult) {
if ($postresult->post_date != '0000-00-00 00:00:00') {
$url = get_permalink($postresult->ID);
$arc_title = $postresult->post_title;
if ($arc_title)
$text = wptexturize(strip_tags($arc_title));
else
$text = $postresult->ID;
$title_text = 'View this post, "' . wp_specialchars($text, 1) . '"';
$output .= '<li>' . mysql2date('d日', $postresult->post_date) . ': ' . "<a href='$url' title='$title_text'>$text</a>";
$output .= ' (' . $postresult->comment_count . ')';
$output .= '</li>' . "\n";
}
}
}
$output .= '</ul></li></ul>' . "\n";
}
update_option('SHe_archives_'.$lastpost,$output);
}else{
$output = '<div class="errorbox">Sorry, no posts matched your criteria.</div>' . "\n";
}
}
echo $output;
}
//分页
if (!function_exists('gk_page')) {
function gk_page($query_string) {
//TUDO 配置页面显示个数
global $posts_per_page, $paged;
$my_query = new WP_Query($query_string . "&posts_per_page=-1");
$total_posts = $my_query->post_count;
if (empty($paged)) $paged = 1;
$prev = $paged - 1;
$next = $paged + 1;
//为0时 只显示 上一页,下一页, 为1时显示最前最后,上一页下一页和数组, 为2时只显示数字
$range = 2;
$showitems = ($range * 2) + 1;
$pages = ceil($total_posts / $posts_per_page);
$start = $paged <= 5 ? 1 : $paged - 5;
$end = $pages < 5 || $paged + 5 > $pages ? $pages : $paged + 5;
if (1 != $pages) {
echo ($paged > 2 && $paged + $range + 1 > $pages && $showitems < $pages) ? "<li><a href='" . get_pagenum_link(1) . "'>最前一页</a></li" : "";
echo ($paged > 1 && $showitems < $pages) ? "<li class='prev'><a href='" . get_pagenum_link($prev) . "'>上一页</a></li>" : "";
for ($i = $start; $i <= $end; $i++) {
$class = '';
if ($paged == $i) $class = ' class="red" style="color:#fff;"';
else $class = ' class="pagination-path"';
echo '<li><a' . $class . ' href="' . get_pagenum_link($i) . '">' . $i . '</a></li>';
}
echo ($paged < $pages && $showitems < $pages) ? "<li class='next'><a href='" . get_pagenum_link($next) . "'>下一页</a></li>" : "";
echo ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) ? "<li><a href='" . get_pagenum_link($pages) . "'>最后一页</a></li>" : "";
}
}
}
//加载css
if (!function_exists('gk_load_css')) {
function gk_load_css() {
//注册css
gk_register_styles();
$css_config = gk_config('css');
foreach ($css_config as $css) {
$before = '';
$after = '';
if (is_array($css)) {
//支持 is_page(), is_home() 等函数判断加载
if (is_bool($css[0])) {
if (!$css[0]) continue;
} else {
$before = '<!--[if ' . $css[0] . ']>';
$after = '<![endif]-->';
}
$css = $css[1];
}
$pathinfo = pathinfo($css);
$ext = isset($pathinfo['extension']) ? $pathinfo['extension'] : '';
if ('css' != $ext) {
//列队名称载入
if (wp_style_is($css)) continue; //如果已经有列队,则不会重复载入
global $wp_styles;
$css_url = $wp_styles->registered[$css]->src;
} else {
$css_url = filter_var($css, FILTER_VALIDATE_URL) ? $css : get_gk_url($css);
}
//echo $before . PHP_EOL;
echo '<link rel="stylesheet" type="text/css" href="' . $css_url . '" media="all">' . PHP_EOL;
//echo $after . PHP_EOL;
}
}
}
if (!function_exists('gk_register_styles')) {
function gk_register_styles() {
$register_css = gk_config('register_css');
foreach ($register_css as $name => $r_css) {
if (!filter_var($r_css, FILTER_VALIDATE_URL)) $r_css = get_gk_url($r_css);
wp_deregister_style($name);
wp_register_style($name, $r_css);
}
}
}
//注册script
add_action('wp_enqueue_scripts', 'gk_register_scripts');
if (!function_exists('gk_register_scripts')) {
function gk_register_scripts() {
$register_js = gk_config('register_js');
foreach ($register_js as $name => $js) {
if (!filter_var($js, FILTER_VALIDATE_URL)) $js = get_gk_url($js);
wp_deregister_script($name);
wp_register_script($name, $js);
}
}
}
//加载js
if (!function_exists('gk_load_js')) {
function gk_load_js() {
$js_config = gk_config('js');
foreach ($js_config as $js) {
$before = '';
$after = '';
if (is_array($js)) {
//支持 is_page(), is_home() 等函数判断加载
if (is_bool($js[0])) {
if (!$js[0]) continue;
} else {
$before = '<!--[if ' . $js[0] . ']>';
$after = '<![endif]-->';
}
$js = $js[1];
}
$pathinfo = pathinfo($js);
$ext = isset($pathinfo['extension']) ? $pathinfo['extension'] : '';
if ('js' != $ext) {
//用列队名称载入
if (wp_script_is($js)) continue; //如果已经载入,则不再重复载入
global $wp_scripts;
$js_url = $wp_scripts->registered[$js]->src;
} else {
$js_url = filter_var($js, FILTER_VALIDATE_URL) ? $js : get_gk_url($js);
}
//echo $before . PHP_EOL;
echo '<script type="text/javascript" src="' . $js_url . '"></script>' . PHP_EOL;
//echo $after . PHP_EOL;
}
}
}
//主题选项
add_action('admin_init', 'gk_theme_options_init');
add_action('admin_menu', 'gk_theme_options_add_page');
if (!function_exists('gk_theme_options_init')) {
function gk_theme_options_init() {
//注册设置组
$theme_options = gk_config('theme_options');
foreach ($theme_options as $name => $setting) {
$callback = '';
if (is_string($setting)) $setting = array(
$setting
);
if (isset($setting[1])){
for($i=1;$i<count($setting);$i++){
$callback = $setting[$i];
register_setting($name, $callback);
}
}
}
}
}
if (!function_exists('gk_theme_options_add_page')) {
function gk_theme_options_add_page() {
$theme_options = gk_config('theme_options');
foreach ($theme_options as $name => $setting) {
if (is_string($setting)) $setting = array(
$setting
);
add_theme_page($setting[0], $setting[0], 'edit_theme_options', $name, 'gk_theme_options_show_page');
}
//加载后台静态文件
add_action('admin_print_scripts', 'gk_admin_scripts');
add_action('admin_print_styles', 'gk_admin_styles');
}
}
if (!function_exists('gk_theme_options_show_page')) {
function gk_theme_options_show_page() {
$option_name = basename($_GET['page']);
include get_gk_file('options/' . $option_name . '.php');
}
}
if (!function_exists('gk_admin_scripts')) {
function gk_admin_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_script('gk-upload', get_gk_url('static/js/gk-upload.js'));
}
}
if (!function_exists('gk_admin_styles')) {
function gk_admin_styles() {
wp_enqueue_style('thickbox');
}
}
if (!function_exists('gk_upload')) {
function gk_upload($name, $value, $class = 'gk_upload', $label = '选择文件') {
echo '<input name="' . $name . '" type="text" id="' . $name . '" value="' . $value . '" class="' . $class . '" /><input type="button" name="left_upload_button" class="gk_upload_button" value="' . $label . '" />';
}
}
//显示菜单函数
if (!function_exists('gk_nav_menu')) {
function gk_nav_menu($location) {
$menus = gk_config('nav');
$args = $menus[$location];
$args['theme_location'] = $location;
wp_nav_menu($args);
}
}
//实例化单页walk
add_filter('wp_page_menu_args', 'gk_wp_page_menu_args');
if (!function_exists('gk_wp_page_menu_args')) {
function gk_wp_page_menu_args($args) {
$args['walker'] = new Gk_Page_Walker;
return $args;
}
}
add_filter('comment_form_defaults', 'gk_comment_form_defaults');
function gk_comment_form_defaults($defaults) {
return wp_parse_args(array(
'comment_field' => '<div class="comment-form-comment control-group"><label class="control-label" for="comment">' . _x('评论内容', 'noun', 'gkwp') . '</label><div class="controls"><textarea class="span6" id="comment" name="comment" rows="8" aria-required="true"></textarea></div></div>',
'comment_notes_before' => '',
'comment_notes_after' => '<div class="form-allowed-tags control-group"><label class="control-label">' . sprintf(__('您可以使用这些 <abbr title="HyperText Markup Language">HTML</abbr> 标签和属性: %s', 'gkwp') , '</label><div class="controls"><pre>' . allowed_tags() . '</pre></div>') . '</div>',
'title_reply' => '<legend>' . __('评论', 'gkwp') . '</legend>',
'title_reply_to' => '<legend>' . __('回复 %s', 'gkwp') . '</legend>',
'must_log_in' => '<div class="must-log-in control-group controls">' . sprintf(__('你必须先 <a href="%s">登录</a>', 'gkwp') , wp_login_url(apply_filters('the_permalink', get_permalink(get_the_ID())))) . '</div>',
'logged_in_as' => '<div class="logged-in-as control-group controls">' . sprintf(__('已登录 <a href="%1$s">%2$s</a>. <a href="%3$s" title="退出">退出?</a>', 'gkwp') , admin_url('profile.php') , wp_get_current_user()->display_name, wp_logout_url(apply_filters('the_permalink', get_permalink(get_the_ID())))) . '</div>',
) , $defaults);
}
// 获得banner_slide
function get_banner_slide(){
$previous_posts = get_posts('numberposts=3&meta_key=banne_slide&meta_value=on');
$str = "";
if(is_array($previous_posts)){
foreach($previous_posts as $post){
setup_postdata($post);
$thumbnail_img = get_post_thumbnail_url($post->ID);
$str.="<li><img src='".$thumbnail_img."' alt='".$post->post_title."' /><div class='flex-caption'><h3>".mb_strimwidth($post->post_title,0,40)."</h3> <p>".strip_tags(mb_strimwidth($post->post_content,0,120,'......'))."</p><input type='button' value='查看详情' class='red' onclick=\"location.href='".get_permalink($post->ID)."'\"/></div></li>";
}
}
echo $str;
}
//获取文章第一张图片,如果没有图就会显示默认的图
function catch_that_image($post="") {
if($post == ""){
global $post, $posts;
}
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
$random = mt_rand(1, 10);
$first_img = get_bloginfo ( 'stylesheet_directory' ).'/static/img/random/'.$random.'.jpg';
}
return $first_img;
}
//获取文章特色图片
function get_post_thumbnail_url($post_id){
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
$thumbnail_id = get_post_thumbnail_id($post_id);
if($thumbnail_id ){
$thumb = wp_get_attachment_image_src($thumbnail_id, 'full');
return $thumb[0];
}else{
return false;
}
}
// 获得main-services 文章列表
function get_main_services_post(){
$previous_posts = get_posts('numberposts=4&meta_key=main_services&meta_value=on&order=asc');
$str = "";
$i = 1 ;
if(is_array($previous_posts)){
foreach($previous_posts as $post){
setup_postdata($post);
$post_title = mb_strimwidth($post->post_title,0,16);
$post_content = mb_strimwidth($post->post_content,0,120);
$str.="<li class='bt-30'> <img src='".get_bloginfo('template_url')."/static/img/".$i."_A.png' alt='".$post_title."'
title='".$post_title."'/><a href='".get_permalink($post->ID)."'><h4 title='阅读更多'>".$post_title."</h4></a><p>".$post_content."</p><a href='".get_permalink($post->ID)."'>— 阅读更多 —</a> </li>";
$i++;
}
}
echo $str;
}
//获取随机文章
function Get_Random_Post($limit=5,$cut_length=44){
global $wpdb;
$randposts = $wpdb->get_results("
SELECT p.ID, p.post_title, rand()*p1.id AS o_id FROM $wpdb->posts AS p JOIN ( SELECT MAX(ID) AS id FROM $wpdb->posts WHERE post_type='post' AND post_status='publish') AS p1 WHERE p.post_type='post' AND p.post_status='publish' ORDER BY o_id LIMIT $limit
");//数据库查询
foreach($randposts as $randpost) {
echo '<li><a href="' . get_permalink($randpost->ID) . '" title="' . $randpost->post_title . '">' . cut_str($randpost->post_title,$cut_length) . '</a></li>';
}//遍历数组输出结果
}
// 获得热评文章
function simple_get_most_viewed($posts_num=8){
global $wpdb;
$sql = "SELECT ID,post_title,post_date,comment_count,post_content
FROM $wpdb->posts
WHERE post_type = 'post'
AND ($wpdb->posts.`post_status` = 'publish' OR $wpdb->posts.`post_status` = 'inherit')
ORDER BY comment_count DESC LIMIT 0 , $posts_num ";
$str = "";
$posts = $wpdb->get_results($sql);
if(is_array($posts)){
foreach($posts as $post){
$str .="<li><a href='".get_permalink($post->ID)."'><h4>".mb_strimwidth($post->post_title,0,38).
"</h4></a><span class='date'>".$post->post_date." ".$post->comment_count." 条评论</span><p>".strip_tags(mb_strimwidth($post->post_content,0,150,'......'))."</p></li>";
}
}
echo $str;
}
//评论列表
if (!function_exists('lovnvns_comment')):
function lovnvns_comment($comment, $args, $depth)
{
$GLOBALS['comment'] = $comment;
switch ($comment->comment_type):
case '':
//主评论计数器初始化 begin
global $commentcount;
$page = (!empty($in_comment_loop)) ? get_query_var('cpage') :
get_page_of_comment($comment->comment_ID, $args);
$cpp = get_option('comments_per_page'); //获取每页评论显示数量
if (!$commentcount) { //初始化楼层计数器
if ($page > 1) {
$commentcount = $cpp * ($page - 1);
} else {
$commentcount = 0; //如果评论还没有分页,初始值为0
}
}
//主评论计数器初始化 end
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<div id="comment-<?php comment_ID(); ?>">
<div class="comment_author">
<?php echo get_avatar($comment, 32); ?>
</div>
<?php if ($comment->comment_approved == '0'): ?>
<p style="color:#e75814"><?php _e('您的评论正在等待审核中...', 'lovnvns'); ?></p><br />
<?php endif; ?>
<p class="commet_text">
<?php
if ($comment->user_id == 1)
echo "<span style='color:#ff6600'>【管理员】</span>";
printf(__('%s:', 'lovnvns'), sprintf('%s', get_comment_author_link())); ?>
<div class="floor"><!-- 主评论楼层号 -->
<?php if (!$parent_id = $comment->comment_parent) {
printf('%1$s', ++$commentcount);
echo "楼";
} ?>
</div>
<?php
printf(__('%1$s %2$s', 'lovnvns'), get_comment_date(), get_comment_time()); ?> <?php comment_reply_link(array_merge
($args, array('depth' => $depth, 'max_depth' => $args['max_depth']))); ?>
<br />
<?php comment_text(); ?>
</p>
</div>
<?php
break;
case 'pingback':
case 'trackback':
?>
<li class="post pingback">
<p><?php _e('Pingback:', 'lovnvns'); ?> <?php comment_author_link(); ?></p>
<?php
break;
endswitch;
}
endif;
//获取最新评论
function Get_Recent_Comment($limit=16,$cut_length=24){
global $wpdb;
$admin_email = "'" . get_bloginfo ('admin_email') . "'"; //获取管理员邮箱,以便排除管理员的评论
$rccdb = $wpdb->get_results("
SELECT ID, post_title, comment_ID, comment_author, comment_author_email, comment_content
FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts
ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
WHERE comment_approved = '1'
AND comment_type = ''
AND post_password = ''
AND comment_author_email != $admin_email
ORDER BY comment_date_gmt
DESC LIMIT $limit
");//数据库查询获得想要的结
foreach ($rccdb as $row) {
$rcc .= "<li>".get_avatar($row,$size='32')."<span class='comment-author'>".$row->comment_author .":</span>"."<br />". "<span class='comment-content'><a href='"
. get_permalink($row->ID) . "#comment-" . $row->comment_ID
. "' title='查看 " . $row->post_title . "'>" . cut_str($row->comment_content,$cut_length)."</a></span>". "</li>";
}
$rcc = convert_smilies($rcc);//允许评论内容中显示表情
echo $rcc;//输出结果
}
//评论表情
function wp_smilies() {
global $wpsmiliestrans;
if ( !get_option('use_smilies') or (empty($wpsmiliestrans))) return;
$smilies = array_unique($wpsmiliestrans);
$link='';
foreach ($smilies as $key => $smile) {
$file = get_bloginfo('template_directory').'/static/img/smilies/'.$smile;
$value = " ".$key." ";
$img = "<img src=\"{$file}\" alt=\"{$smile}\" />";
$imglink = htmlspecialchars($img);
$link .= "<a href=\"#commentform\" title=\"{$smile}\" onclick=\"document.getElementById('comment').value += '{$value}'\">{$img}</a> ";
}
echo $link;
}
//修改表情默认路径
add_filter('smilies_src','custom_smilies_src',1,10);
function custom_smilies_src ($img_src, $img, $siteurl){
return get_bloginfo('template_directory').'/static/img/smilies/'.$img;
}
//彩色标签云
function colorCloud($text) {
$text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);
return $text;
}
function colorCloudCallback($matches) {
$text = $matches[1];
for($a=0;$a<6;$a++){ //采用#ffffff方法
$color.=dechex(rand(0,15));//累加随机的数据--dechex()将十进制改为十六进制
}
$pattern = '/style=(\'|\")(.*)(\'|\")/i';
$text = preg_replace($pattern, "style=\"color:#{$color};$2;\"", $text);
return "<a $text>";
unset($color);//卸载color
}
/**
* 取消下面的注释即可开启彩色标签云
* add_filter('wp_tag_cloud', 'colorCloud', 1);
* */
//标题文字截断
function cut_str($src_str,$cut_length){
$return_str='';
$i=0;
$n=0;
$str_length=strlen($src_str);
while (($n<$cut_length) && ($i<=$str_length)){
$tmp_str=substr($src_str,$i,1);
$ascnum=ord($tmp_str);
if ($ascnum>=224){
$return_str=$return_str.substr($src_str,$i,3);
$i=$i+3;
$n=$n+2;
}
elseif ($ascnum>=192){
$return_str=$return_str.substr($src_str,$i,2);
$i=$i+2;
$n=$n+2;
}
elseif ($ascnum>=65 && $ascnum<=90){
$return_str=$return_str.substr($src_str,$i,1);
$i=$i+1;
$n=$n+2;
}
else {
$return_str=$return_str.substr($src_str,$i,1);
$i=$i+1;
$n=$n+1;
}
}
if ($i<$str_length){
$return_str = $return_str . '';
}
if (get_post_status() == 'private'){
$return_str = $return_str . '(private)';
}
return $return_str;
}
//开启后台友情链接
add_filter( 'pre_option_link_manager_enabled', '__return_true' );
/**
* DChaser 主题 functions.php 到此结束
* 如仍有内容,可能感染木马,删除即可
**/