主要functions.php功能解释
引入功能 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20…
引入功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//00 引入功能
ob_start();
include_once(“functions/options/data_cache.php”); //主题设置变量获取
include_once(“functions/options/main.php”); //主题选项设置
include_once(“functions/options/customize_css.php”); //主题选项css设置
include_once(“functions/breadcrumbs/get_breadcrumbs.php”); //面包屑导航
include_once(“functions/pagination/par_pagenavi.php”); //分页
include_once(“functions/themeoption/themeoption.php”); //后台主题选项设置
include_once(“functions/walker/wp_bootstrap_navwalker.php”); //自定义菜单
include_once(“functions/meta-box/article_box.php”); //文章自定义字段(seo选项)
include_once(“functions/meta-box/category_box.php”); //栏目自定义字段
include_once(“functions/meta-box/tab.php”); //产品页tab自定义字段
include_once(“functions/multi-image-upload/multi-image-upload.php”); //上传多张图片
include_once(“functions/read.php”); //阅读统计功能
include_once(“functions/comments.php”); //评论模板
include_once(“functions/thumbnails.php”); //缩略图功能
include_once(“functions/author-avatars.php”); //作者信息及头像
include_once(“functions/post-type/slide.php”); //相册/首页banner功能
include_once(“functions/post-type/products.php”); //自定义产品post-type
include_once(“functions/admin_edit.php”); //后台及头部代码
include_once(“functions/system_edit.php”); //自定义登录及后台
include_once(“functions/woocommerce.php”); //woocommerce插件相关
|
其他包含
自定义缩略图大小
1
2
3
4
5
6
7
|
//01自定义缩略图大小,前台调取方法 the_post_thumbnail(‘two’);
add_theme_support( ‘post-thumbnails’ );
add_image_size( ‘one’, 370,250, true ); // 新闻、列表尺寸
add_image_size( ‘two’, 400,110, true ); // 相关文章
add_image_size( ‘three’, 670, 450, true ); // 新闻、列表尺寸
add_image_size( ‘four’, 383, 435, true ); // 案例图片尺寸
add_image_size( ‘five’, 600, 600, true ); // 案例图片尺寸
|
注册菜单栏
1
2
3
4
5
6
|
//02 注册菜单栏,也就是在WP后台-外观-菜单-主题位置,如果主题包含多个菜单,可以在array里面增加。
register_nav_menus( array(
‘primary’ => __( ‘Primary Navigation’, ‘twentyten’ ),
‘top-right-menu’=> __(‘Top Right Menu’,‘twentyten’),
‘footer-navigation’=> __(‘Footer Navigation’,‘twentyten’),
) );
|
自定义文章形式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//03 文章形式
/**
*文章形式(Post Format)
*standard:WordPress默认的标准格式
*aside:日志,没有标题的文章,类似Facebook的update;
*image:图片,一般情况下是只有标题的和一张图片;
*gallery:相册,在文章中插入一个使用短代码生成的相册;
*quote:引用,一般将作者和出处当作标题;
*status:状态,类似微博,简短且没有标题;
*link:链接,分享了一个链接,通常是标题是链接名称,内容中填写URL;
*video:视频
*audio:音频
*chat:聊天记录,类似下面这种格式:
**/
// 支持Post Format。可自定义发布类型。
add_theme_support( ‘post-formats’, array( ‘aside’, ‘status’, ‘gallery’ ) );
//修改发布类型名称
function rename_post_formats( $safe_text )
{ if ( $safe_text == ‘Aside’ ) return ‘Services’;if ( $safe_text == ‘Status’ ) return ‘Projects’; return $safe_text; }
add_filter( ‘esc_html’, ‘rename_post_formats’ );
|
使子分类的category页面渲染父category页面的模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//04 wordpress 使子分类的category页面渲染父category页面的模板
add_filter(‘category_template’, ‘f_category_template’);
function f_category_template($template){
$category = get_queried_object();
if($category->parent !=‘0’){
while($category->parent !=‘0’){
$category = get_category($category->parent);
}
}
$templates = array();
if ( $category ) {
$templates[] = “category-{$category->slug}.php”;
$templates[] = “category-{$category->term_id}.php”;
}
$templates[] = ‘category.php’;
return locate_template( $templates );
}
|
自定义原生相册的样式
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
|
//05 自定义原生相册的样式
add_shortcode(‘gallery’, ‘my_gallery_shortcode’);
function my_gallery_shortcode($attr) {
$post = get_post();
static $instance = 0;
$instance++;
if ( ! empty( $attr[‘ids’] ) ) {
// ‘ids’ is explicitly ordered, unless you specify otherwise.
//除非另有说明,否则’ids’是明确排序的
if ( empty( $attr[‘orderby’] ) )
$attr[‘orderby’] = ‘post__in’;
$attr[‘include’] = $attr[‘ids’];
}
// Allow plugins/themes to override the default gallery template.
//允许插件/主题覆盖默认的图库模板
$output = apply_filters(‘post_gallery’, ”, $attr);
if ( $output != ” )
return $output;
// We’re trusting author input, so let’s at least make sure it looks like a valid orderby statement
//我们信任作者的输入,因此至少要确保它看起来像是有效的orderby语句
if ( isset( $attr[‘orderby’] ) ) {
$attr[‘orderby’] = sanitize_sql_orderby( $attr[‘orderby’] );
if ( !$attr[‘orderby’] )
unset( $attr[‘orderby’] );
}
extract(shortcode_atts(array(
‘order’ => ‘ASC’,
‘orderby’ => ‘menu_order ID’,
‘id’ => $post->ID,
‘itemtag’ => ‘dl’,
‘icontag’ => ‘dt’,
‘captiontag’ => ‘dd’,
‘columns’ => 3,
‘size’ => ‘thumbnail’,
‘include’ => ”,
‘exclude’ => ”
), $attr));
$id = intval($id);
if ( ‘RAND’ == $order )
$orderby = ‘none’;
if ( !empty($include) ) {
$_attachments = get_posts( array(‘include’ => $include, ‘post_status’ => ‘inherit’, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘order’ => $order, ‘orderby’ => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$attachments = get_children( array(‘post_parent’ => $id, ‘exclude’ => $exclude, ‘post_status’ => ‘inherit’, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘order’ => $order, ‘orderby’ => $orderby) );
} else {
$attachments = get_children( array(‘post_parent’ => $id, ‘post_status’ => ‘inherit’, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘order’ => $order, ‘orderby’ => $orderby) );
}
if ( empty($attachments) )
return ”;
if ( is_feed() ) {
$output = “n”;
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . “n”;
return $output;
}
//定义html标签符
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$icontag = tag_escape($icontag);
$valid_tags = wp_kses_allowed_html( ‘post’ );
if ( ! isset( $valid_tags[ $itemtag ] ) )
$itemtag = ‘dl’;
if ( ! isset( $valid_tags[ $captiontag ] ) )
$captiontag = ‘dd’;
if ( ! isset( $valid_tags[ $icontag ] ) )
$icontag = ‘dt’;
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? ‘right’ : ‘left’;
$selector = “gallery-{$instance}”;
$gallery_style = $gallery_div = ”;
$gallery_style = $gallery_div = ”;
if ( apply_filters( ‘use_default_gallery_style’, true ) )
$size_class = sanitize_html_class( $size );
if($columns==6){
$colclass=2;
}
else if($columns==4){
$colclass=3;
}
else if($columns==3){
$colclass=4;
}
else if($columns==2){
$colclass=6;
}
else{
$colclass=4;
}
//开始输出
$gallery_div = “<div id=’$selector’ class=’docs-galley gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}’>n”;
$output = apply_filters( ‘gallery_style’, $gallery_style . “ntt” . $gallery_div );
$output .= “<div class=’row docs-pictures clearfix beijing01′>n”;
$i = 0;
foreach ( $attachments as $id => $attachment ) {
//$link = isset($attr[‘link’]) && ‘file’ == $attr[‘link’] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$link1 = wp_get_attachment_image_src( $id, $size, false);
$link2 = wp_get_attachment_image( $id, “one”, “”, array( “data-original” => $link1[0] ) );
$alt = get_post_meta( $id, ‘_wp_attachment_image_alt’, true );
$image_title = get_the_title($id);
$caption = get_the_content($id);
$description = get_the_excerpt($id);
$output .= “<div class=’col-md-{$colclass} col-xs-6′>”;
$output .= “<{$itemtag} class=’wp-imgmain’>”;
$output .= “<{$icontag} class=’gallery-icon’>”;
$output .= “<a href=’$link1[0]’ class=’fancybox’>$link2</a>”;
//当图片有简介,输出简介
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= “<{$captiontag} class=’wp-caption-text gallery-caption’>” . wptexturize($attachment->post_excerpt) . “</{$captiontag}>”;
}
$output .= “</{$icontag}>”;
$output .= “<h4 class=’wp-title’>$image_title</h4>”;
$output .= “</{$itemtag}>”;
$output .= “</div>”;
//当数量为columns的输出,后面添加内容
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= ‘<br style=”clear: both” />’;
}
$output .= “</div>n”;
$output .= “<br style=’clear: both;’ /></div>n”;
return $output;
}
|
类别:WordPress开发、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!