forked from pagecho/maupassant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
155 lines (139 loc) · 5.56 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
<?php
/**
*
* @package Maupassant
* @author cho, grafting by mufeng
* @version 1.14
* @link http://pagecho.com
*/
// 定义主题路径
define( "THEMEPATH", get_bloginfo('template_directory') );
// 定义主题版本号
define( "THEMEVERSION", '1.14' );
// 添加RSS
add_theme_support( 'automatic-feed-links' );
// 定义菜单
register_nav_menus(
array(
'primary' => __('主题菜单')
)
);
// Enqueue style-file, if it exists.
add_action('wp_enqueue_scripts', 'ms_scripts');
function ms_scripts() {
global $wp_scripts;
wp_enqueue_style('normalize', THEMEPATH . '/css/normalize.css', array(), THEMEVERSION, 'screen');
wp_enqueue_style('style', get_bloginfo('stylesheet_url'), array(), THEMEVERSION, 'screen');
wp_register_script( 'html5shiv', 'http://x.papaapp.com/farm1/a571d2/8dda131d/html5shiv.js',array(), '3.7.1');
$wp_scripts->add_data( 'html5shiv', 'conditional', 'lt IE 9' );
wp_enqueue_script('html5shiv');
}
// Maupassant's widgets
function ms_widgets() {
register_sidebar(array(
'name' => 'sidebar1',
'description' => __('主题侧边栏'),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
));
}
add_action( 'widgets_init', 'ms_widgets' );
// Pagenavi of archive and index part
function pagenavi( $p = 5 ) {
if ( is_singular() ) return;
global $wp_query, $paged;
$max_page = $wp_query->max_num_pages;
if ( $max_page == 1 ) return;
echo '<ol class="page-navigator">';
if ( empty( $paged ) ) $paged = 1;
if ( $paged > 1 ) p_link( $paged - 1, '« Previous', '« Previous' );
if ( $paged > $p + 2 ) echo '<li><span>...</span></li>';
for( $i = $paged - $p; $i <= $paged + $p; $i++ ) {
if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<li class='current'><span>{$i}</span></li>" : p_link( $i );
}
if ( $paged < $max_page - $p - 1 ) echo '<li><span>...</span></li>';
if ( $paged < $max_page ) p_link( $paged + 1,'Next »', 'Next »' );
echo '</ol>';
}
function p_link( $i, $title = '', $linktype = '' ) {
if ( $title == '' ) $title = "第 {$i} 页";
if ( $linktype == '' ) { $linktext = $i; } else { $linktext = $linktype; }
echo "<li><a href='", esc_html( get_pagenum_link( $i ) ), "' title='{$title}'>{$linktext}</a></li>";
}
function ms_head() {
?>
<?php if ( is_home() ) { ?><title><?php bloginfo('name'); ?> - <?php bloginfo('description'); ?></title><?php } ?>
<?php if ( is_search() ) { ?><title><?php _e('搜索"');the_search_query();echo """;?> - <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_single() ) { ?><title><?php echo trim(wp_title('',0)); ?> - <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_author() ) { ?><title><?php wp_title(""); ?> - <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_archive() ) { ?><title><?php single_cat_title(); ?> - <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_year() ) { ?><title><?php the_time('Y'); ?> - <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_month() ) { ?><title><?php the_time('F'); ?> - <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_page() ) { ?><title><?php echo trim(wp_title('',0)); ?> - <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_404() ) { ?><title>404 - <?php bloginfo('name'); ?></title><?php } ?>
<?php
global $post;
if (is_home()){
$keywords = '';
$description = '';
}elseif (is_single()){
$keywords = get_post_meta($post->ID, "keywords", true);
if($keywords == ""){
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag){
$keywords = $keywords.$tag->name.",";
}
$keywords = rtrim($keywords, ', ');
}
$description = get_post_meta($post->ID, "description", true);
if($description == ""){
if($post->post_excerpt){
$description = $post->post_excerpt;
}else{
$description = mb_strimwidth(strip_tags($post->post_content),0,200,'');
}
}
}elseif (is_page()){
$keywords = get_post_meta($post->ID, "keywords", true);
$description = get_post_meta($post->ID, "description", true);
}elseif (is_category()){
$keywords = single_cat_title('', false);
$description = category_description();
}elseif (is_tag()){
$keywords = single_tag_title('', false);
$description = tag_description();
}
$keywords = trim(strip_tags($keywords));
$description = trim(strip_tags($description));
?>
<meta name="keywords" content="<?php echo $keywords; ?>" />
<meta name="description" content="<?php echo $description; ?>" />
<meta name="viewport" content="initial-scale=1.0,user-scalable=no">
<link rel="shortcut icon" href="<?php bloginfo('url'); ?>/favicon.ico" type="image/x-icon" />
<?php wp_head();?>
<?php
}
function ms_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<div id="comment-<?php comment_ID(); ?>" class="comment-body">
<div class="comment-author">
<?php echo get_avatar( $comment, $size = '32'); ?>
<cite class="fn"><?php printf(__('%s'), get_comment_author_link()) ?></cite>
</div>
<div class="comment-meta">
<?php printf(__('%s'), get_comment_date("Y/m/d") ) ?>
</div>
<div class="comment-content">
<p><?php comment_text() ?></p>
</div>
<div class="comment-reply">
<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth'], 'reply_text' => __('回复')))) ?>
</div>
</div>
<?php
}
?>