wordpress二次开发 中对于functions.php的制作是非常多的,今天我们来分享关于wordpress二次开发技巧之functions.php的制作,希望大家可以喜欢!
下面根据需求,对各种能实现进行简单介绍。
先对functions.php文件进行介绍,通过代码实现各功能。
1. widgets sidebar 侧边栏小工具
wordpress二次开发技巧之functions.php篇
02
if
( function_exists(
'register_sidebar'
class
=
"crayon-sy"
>) ) {
03
register_sidebar(
array
(
04
'name'
class
=
"crayon-o"
>=>
'First_sidebar'
class
=
"crayon-sy"
>,
05
'before_widget'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>,
06
'after_widget'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>,
07
'before_title'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>,
08
'after_title'
class
=
"crayon-o"
>=>
''
10
register_sidebar(
array
(
11
'name'
class
=
"crayon-o"
>=>
'Second_sidebar'
class
=
"crayon-sy"
>,
12
'before_widget'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>,
13
'after_widget'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>,
14
'before_title'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>,
15
'after_title'
class
=
"crayon-o"
>=>
''
17
register_sidebar(
array
(
18
'name'
class
=
"crayon-o"
>=>
'Third_sidebar'
class
=
"crayon-sy"
>,
19
'before_widget'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>,
20
'after_widget'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>,
21
'before_title'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>,
22
'after_title'
class
=
"crayon-o"
>=>
''
24
register_sidebar(
array
(
25
'name'
class
=
"crayon-o"
>=>
'Fourth_sidebar'
class
=
"crayon-sy"
>,
26
'before_widget'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>,
27
'after_widget'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>,
28
'before_title'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>,
29
'after_title'
class
=
"crayon-o"
>=>
''
33
register_nav_menus(
array
(
"primary"
=>
"Primary Navigation"
));
2. 后台支持自定义菜单
wordpress二次开发技巧之functions.php篇
02
if
(function_exists(
'register_nav_menus'
class
=
"crayon-sy"
>)){
05
'header-menu'
class
=
"crayon-o"
>=> __(
'导航自定义菜单'
class
=
"crayon-sy"
>),
06
'footer-menu'
class
=
"crayon-o"
>=> __(
'页角自定义菜单'
class
=
"crayon-sy"
>),
07
'sider-menu'
class
=
"crayon-o"
>=> __(
'侧边栏菜单'
class
=
"crayon-sy"
>),
08
'primary'
class
=
"crayon-o"
>=> __(
'Primary Navigation'
class
=
"crayon-sy"
>,
'lee'
class
=
"crayon-sy"
>),
输出菜单,在如header.php插入显示菜单的代码
1
array
(
'container'
class
=
"crayon-o"
>=>
''
class
=
"crayon-sy"
>) ); ?>
3. 面包屑导航
02
function
wheatv_breadcrumbs() {
03
$delimiter
=
'> '
class
=
"crayon-sy"
>;
04
$name
=
'首页'
class
=
"crayon-sy"
>;
05
$currentBefore
=
''
class
=
"crayon-sy"
>;
06
$currentAfter
=
''
class
=
"crayon-sy"
>;
08
if
( !is_home() && !is_front_page() || is_paged() ) {
10
echo
''
class
=
"crayon-sy"
>;
13
$home
= get_option(
'home'
class
=
"crayon-sy"
>);
16
if
( is_category() ) {
18
$cat_obj
=
$wp_query
->get_queried_object();
19
$thisCat
=
$cat_obj
->term_id;
20
$thisCat
= get_category(
$thisCat
);
21
$parentCat
= get_category(
$thisCat
->parent);
22
if
(
$thisCat
->parent != 0)
echo
(get_category_parents(
$parentCat
, TRUE,
' '
class
=
"crayon-sy"
>.
$delimiter
.
' '
class
=
"crayon-sy"
>));
23
echo
$currentBefore
.
''
class
=
"crayon-sy"
>;
25
echo
''
class
=
"crayon-sy"
>.
$currentAfter
;
27
}
elseif
( is_day() ) {
28
echo
''
class
=
"crayon-sy"
>. get_the_time(
'Y'
class
=
"crayon-sy"
>) .
' '
class
=
"crayon-sy"
>.
$delimiter
.
' '
class
=
"crayon-sy"
>;
29
echo
''
class
=
"crayon-sy"
>. get_the_time(
'F'
class
=
"crayon-sy"
>) .
' '
class
=
"crayon-sy"
>.
$delimiter
.
' '
class
=
"crayon-sy"
>;
30
echo
$currentBefore
. get_the_time(
'd'
class
=
"crayon-sy"
>) .
$currentAfter
;
32
}
elseif
( is_month() ) {
33
echo
''
class
=
"crayon-sy"
>. get_the_time(
'Y'
class
=
"crayon-sy"
>) .
' '
class
=
"crayon-sy"
>.
$delimiter
.
' '
class
=
"crayon-sy"
>;
34
echo
$currentBefore
. get_the_time(
'F'
class
=
"crayon-sy"
>) .
$currentAfter
;
36
}
elseif
( is_year() ) {
37
echo
$currentBefore
. get_the_time(
'Y'
class
=
"crayon-sy"
>) .
$currentAfter
;
39
}
elseif
( is_single() ) {
40
$cat
= get_the_category();
$cat
=
$cat
[0];
41
echo
get_category_parents(
$cat
, TRUE,
' '
class
=
"crayon-sy"
>.
$delimiter
.
' '
class
=
"crayon-sy"
>);
46
}
elseif
( is_page() && !
$post
->post_parent ) {
51
}
elseif
( is_page() &&
$post
->post_parent ) {
52
$parent_id
=
$post
->post_parent;
53
$breadcrumbs
=
array
();
55
$page
= get_page(
$parent_id
);
56
$breadcrumbs
[] =
''
class
=
"crayon-sy"
>. get_the_title(
$page
->ID) .
''
class
=
"crayon-sy"
>;
57
$parent_id
=
$page
->post_parent;
59
$breadcrumbs
=
array_reverse
(
$breadcrumbs
);
60
foreach
(
$breadcrumbs
as
$crumb
)
echo
$crumb
.
' '
class
=
"crayon-sy"
>.
$delimiter
.
' '
class
=
"crayon-sy"
>;
65
}
elseif
( is_search() ) {
66
echo
$currentBefore
.
'搜索结果'
class
=
"crayon-sy"
>. get_search_query() .
''
class
=
"crayon-sy"
>.
$currentAfter
;
68
}
elseif
( is_tag() ) {
69
echo
$currentBefore
.
'搜索标签: '
class
=
"crayon-sy"
>;
71
echo
''
class
=
"crayon-sy"
>.
$currentAfter
;
73
}
elseif
( is_author() ) {
75
$userdata
= get_userdata(
$author
);
76
echo
$currentBefore
.
'Articles posted by '
class
=
"crayon-sy"
>.
$userdata
->display_name .
$currentAfter
;
78
}
elseif
( is_404() ) {
79
echo
$currentBefore
.
'Error 404'
class
=
"crayon-sy"
>.
$currentAfter
;
82
if
( get_query_var(
'paged'
class
=
"crayon-sy"
>) ) {
83
if
( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
echo
' ('
class
=
"crayon-sy"
>;
84
echo
__(
'第'
class
=
"crayon-sy"
>) .
''
class
=
"crayon-sy"
>. get_query_var(
'paged'
class
=
"crayon-sy"
>) .
'页'
class
=
"crayon-sy"
>;
85
if
( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
echo
')'
class
=
"crayon-sy"
>;
87
echo
''
class
=
"crayon-sy"
>;
显示面包屑导航(category.php或single.php等)
4. 文章访问量(点击数)
wordpress二次开发技巧之functions.php篇
02
function
getPostViews(
$postID
){
03
$count_key
=
'post_views_count'
class
=
"crayon-sy"
>;
04
$count
= get_post_meta(
$postID
,
$count_key
, true);
05
if
(
$count
==
''
class
=
"crayon-sy"
>){
06
delete_post_meta(
$postID
,
$count_key
);
07
add_post_meta(
$postID
,
$count_key
,
'0'
class
=
"crayon-sy"
>);
12
function
setPostViews(
$postID
) {
13
$count_key
=
'post_views_count'
class
=
"crayon-sy"
>;
14
$count
= get_post_meta(
$postID
,
$count_key
, true);
15
if
(
$count
==
''
class
=
"crayon-sy"
>){
17
delete_post_meta(
$postID
,
$count_key
);
18
add_post_meta(
$postID
,
$count_key
,
'0'
class
=
"crayon-sy"
>);
21
update_post_meta(
$postID
,
$count_key
,
$count
);
显示点击量(如category.php或single.php)
1
echo
getPostViews(get_the_ID()); ?>
5. 文章中所有链接新窗口中打开
2
function
autoblank(
$content
) {
3
$content
= preg_replace(
"//"
,
"_blank
">"
,
$content
);
6
add_filter(
'the_content'
class
=
"crayon-sy"
>,
'autoblank'
class
=
"crayon-sy"
>);
6. 清除wp自带无用头部信息
03
remove_action(
'wp_head'
class
=
"crayon-sy"
>,
'feed_links'
class
=
"crayon-sy"
>, 2 );
04
remove_action(
'wp_head'
class
=
"crayon-sy"
>,
'feed_links_extra'
class
=
"crayon-sy"
>, 3 );
05
remove_action(
'wp_head'
class
=
"crayon-sy"
>,
'rsd_link'
class
=
"crayon-sy"
>);
06
remove_action(
'wp_head'
class
=
"crayon-sy"
>,
'wlwmanifest_link'
class
=
"crayon-sy"
>);
07
remove_action(
'wp_head'
class
=
"crayon-sy"
>,
'index_rel_link'
class
=
"crayon-sy"
>);
08
remove_action(
'wp_head'
class
=
"crayon-sy"
>,
'parent_post_rel_link'
class
=
"crayon-sy"
>, 10, 0 );
09
remove_action(
'wp_head'
class
=
"crayon-sy"
>,
'start_post_rel_link'
class
=
"crayon-sy"
>, 10, 0 );
10
remove_action(
'wp_head'
class
=
"crayon-sy"
>,
'adjacent_posts_rel_link_wp_head'
class
=
"crayon-sy"
>, 10, 0 );
12
remove_action(
'publish_future_post'
class
=
"crayon-sy"
>,
'check_and_publish_future_post'
class
=
"crayon-sy"
>, 10, 1 );
16
remove_action(
'wp_head'
class
=
"crayon-sy"
>,
'wp_generator'
class
=
"crayon-sy"
>);
18
remove_action(
'wp_footer'
class
=
"crayon-sy"
>,
'wp_print_footer_scripts'
class
=
"crayon-sy"
>);
19
remove_action(
'wp_head'
class
=
"crayon-sy"
>,
'wp_shortlink_wp_head'
class
=
"crayon-sy"
>, 10, 0 );
20
remove_action(
'template_redirect'
class
=
"crayon-sy"
>,
'wp_shortlink_header'
class
=
"crayon-sy"
>, 11, 0 );
21
add_action(
'widgets_init'
class
=
"crayon-sy"
>,
'my_remove_recent_comments_style'
class
=
"crayon-sy"
>);
22
function
my_remove_recent_comments_style() {
23
global
$wp_widget_factory
;
24
remove_action(
'wp_head'
class
=
"crayon-sy"
>,
array
(
$wp_widget_factory
->widgets[
'WP_Widget_Recent_Comments'
class
=
"crayon-sy"
>],
'recent_comments_style'
class
=
"crayon-sy"
>));
7. 自动保存和文章修订功能
wordpress二次开发技巧之functions.php篇
2
define(
'AUTOSAVE_INTERVAL'
class
=
"crayon-sy"
>, 120 );
3
define(
'WP_POST_REVISIONS'
class
=
"crayon-sy"
>, false);
8. 彩色静态标签云
wordpress二次开发技巧之functions.php篇
02
function
colorCloud(
$text
) {
03
$text
= preg_replace_callback(
'||i'
class
=
"crayon-sy"
>,
'colorCloudCallback'
class
=
"crayon-sy"
>,
$text
);
06
function
colorCloudCallback(
$matches
) {
08
$color
=
dechex
(rand(0,16777215));
09
$pattern
=
'/style=('
|
")(.*)('|"
)/i'class
=
"crayon-sy"
>;
10
$text
= preg_replace(
$pattern
,
"style="
color:#{
$color
};
$2
;
""
,
$text
);
13
add_filter(
'wp_tag_cloud'
class
=
"crayon-sy"
>,
'colorCloud'
class
=
"crayon-sy"
>, 1);
输出标签
1
'smallest=10&largest=14&number=32&order=RAND'
class
=
"crayon-sy"
>) ?>
9. 搜索结果关键词高亮显示
具体实现,再另写文章介绍,后续可通过搜索找到。
wordpress二次开发技巧之functions.php篇
02
function
lee_set_query() {
03
$query
= attribute_escape(get_search_query());
04
if
(
strlen
(
$query
) > 0){
12
function
lee_init_jquery() {
13
wp_enqueue_script(
'jquery'
class
=
"crayon-sy"
>);
15
add_action(
'init'
class
=
"crayon-sy"
>,
'lee_init_jquery'
class
=
"crayon-sy"
>);
16
add_action(
'wp_print_scripts'
class
=
"crayon-sy"
>,
'lee_set_query'
class
=
"crayon-sy"
>);
10. 自定义登录界面
在不更改html的情况下,可通过引入样式文件,自定义登录界面。
wordpress二次开发技巧之functions.php篇
02
function
custom_login(){
03
echo
'class
="crayon-sy
">. get_bloginfo('template_directory'crayon-sy
">) . '/css/login.css"
/>'class
=
"crayon-sy"
>;
05
add_action(
'login_head'
class
=
"crayon-sy"
>,
'custom_login'
class
=
"crayon-sy"
>);
07
function
login_headerurl(
$url
) {
08
return
get_bloginfo(
'url'
class
=
"crayon-sy"
>);
10
add_filter(
'login_headerurl'
class
=
"crayon-sy"
>,
'login_headerurl'
class
=
"crayon-sy"
>);
12
function
login_headertitle(
$title
){
13
return
__(
'轩枫阁'
class
=
"crayon-sy"
>);
15
add_filter(
'login_headertitle'
class
=
"crayon-sy"
>,
'login_headertitle'
class
=
"crayon-sy"
>);
11. 获得当前文章的位置
wordpress二次开发技巧之functions.php篇
wordpress二次开发技巧之functions.php篇
02
function
num_Pcurrent_post() {
04
$postid
= get_the_ID();
05
$getpost
= get_post(
$postid
);
06
$daysago
=
$getpost
->post_date;
07
$today
=
gmdate
(
'Y-m-d H:i:s'
class
=
"crayon-sy"
>, time() + 3600 * 8);
08
$count_posts
= wp_count_posts();
09
$num_all_posts
=
$count_posts
->publish;
10
$result
=
$wpdb
->get_results(
"SELECT ID FROM $wpdb->posts WHERE post_date BETWEEN '$daysago' AND '$today' AND post_status='publish' AND post_type='post' ORDER BY post_date DESC "
);
11
foreach
(
$result
as
$Item
) {
12
$post_ID
[] =
$Item
->ID;
14
$num_current_post
=
count
(
$post_ID
)-1;
15
$output
.=
$num_current_post
.
'/'
class
=
"crayon-sy"
>.
$num_all_posts
;
18
function
num_Ncurrent_post() {
20
$postid
= get_the_ID();
21
$getpost
= get_post(
$postid
);
22
$daysago
=
$getpost
->post_date;
23
$today
=
gmdate
(
'Y-m-d H:i:s'
class
=
"crayon-sy"
>, time() + 3600 * 8);
24
$count_posts
= wp_count_posts();
25
$num_all_posts
=
$count_posts
->publish;
26
$result
=
$wpdb
->get_results(
"SELECT ID FROM $wpdb->posts WHERE post_date BETWEEN '$daysago' AND '$today' AND post_status='publish' AND post_type='post' ORDER BY post_date DESC "
);
27
foreach
(
$result
as
$Item
) {
28
$post_ID
[] =
$Item
->ID;
30
$num_current_post
=
count
(
$post_ID
)+1;
31
$output
.=
$num_current_post
.
'/'
class
=
"crayon-sy"
>.
$num_all_posts
;
显示上下篇及位置,在single.php
1
if
(get_next_post()) {
echo
next_post_link(
'%link'
class
=
"crayon-sy"
>,__(
'上一篇 '
class
=
"crayon-sy"
>,
'lee'
class
=
"crayon-sy"
>),
$in_same_cat
= false,
$excluded_categories
=
'11'
class
=
"crayon-sy"
>); }
else
{
echo
'已最新 '
class
=
"crayon-sy"
>; }?>
5
if
(get_previous_post()) {
echo
previous_post_link(
'%link'
class
=
"crayon-sy"
>,__(
'下一篇 '
class
=
"crayon-sy"
>,
'lee'
class
=
"crayon-sy"
>),
$in_same_cat
= false,
$excluded_categories
=
'11'
class
=
"crayon-sy"
>); }
else
{
echo
'已最后 '
class
=
"crayon-sy"
>; }?>
12. 热门标签ajax加载
点击换一换
wordpress二次开发技巧之functions.php篇
03
if
( isset(
$_GET
[
'action'
class
=
"crayon-sy"
>])){
04
if
(
$_GET
[
'action'
class
=
"crayon-sy"
>] ==
'tag'
class
=
"crayon-h"
> ){
05
echo
wp_tag_cloud(
'smallest=10&largest=14&number=32&order=RAND'
class
=
"crayon-sy"
>);
11
add_action(
'init'
class
=
"crayon-sy"
>,
'tagLoad'
class
=
"crayon-sy"
>);
HTML部分
使用JS实现点击交互
02
$(
".tag_change"
).click(
function
(){
04
url: $(this).attr(
"href"
),
05
type:
'get'
class
=
"crayon-sy"
>,
06
beforeSend:
function
() {
09
error:
function
(error) {
12
success:
function
(data) {
14
$(
".tag_content"
).
empty
().append($(data).fadeIn(200));
13. 外链加上nofollow
防止引入的网站有问题,被降权
02
add_filter(
'the_content'
,
'cn_nf_url_parse'
);
03
function
cn_nf_url_parse(
$content
) {
05
$regexp
=
"]*href=("
??)([^
" >]*?)1[^>]*>"
;
06
if
(preg_match_all(
"/$regexp/siU"
,
$content
,
$matches
, PREG_SET_ORDER)) {
07
if
( !
empty
(
$matches
) ) {
09
$srcUrl
= get_option(
'siteurl'
);
10
for
(
$i
=0;
$i
<
count
(
$matches
);
$i
++)
13
$tag
=
$matches
[
$i
][0];
14
$tag2
=
$matches
[
$i
][0];
15
$url
=
$matches
[
$i
][0];
19
$pattern
=
'/targets*=s*"s*_blanks*"/'
;
20
preg_match(
$pattern
,
$tag2
,
$match
, PREG_OFFSET_CAPTURE);
21
if
(
count
(
$match
) < 1 )
22
$noFollow
.=
' target="_blank" '
;
24
$pattern
=
'/rels*=s*"s*[n|d]ofollows*"/'
;
25
preg_match(
$pattern
,
$tag2
,
$match
, PREG_OFFSET_CAPTURE);
26
if
(
count
(
$match
) < 1 )
27
$noFollow
.=
' rel="nofollow" '
;
29
$pos
=
strpos
(
$url
,
$srcUrl
);
31
$tag
= rtrim (
$tag
,
'>'
);
32
$tag
.=
$noFollow
.
'>'
;
33
$content
=
str_replace
(
$tag2
,
$tag
,
$content
);
39
$content
=
str_replace
(
']]>'
,
']]>'
,
$content
);
14. 图片懒加载lazyload3. 外链加上nofollow
1
function
lazyload(
$content
) {
2
if
(!is_feed()||!is_robots) {
3
$content
=preg_replace(
'/"]([^'"
]+)['
"](.*)>/i',"
$1data
-original=
"$2"
src=
"data:image/gif;base64,R0lGODlhAQABAIAAAOHh4QAAACH5BAAAAAAALAAAAAABAAEAQAICRAEAOw=="
$3
>n",
$content
);
7
add_filter (
'the_content'
,
'lazyload'
);
JS代码(需引入jquery.lazyload.js)
15. 获取文章中第一张图
如跑马灯图
wordpress二次开发技巧之functions.php篇
02
function
wpdx_postimage(
$atts
,
$content
= null) {
03
extract(shortcode_atts(
array
(
07
$images
=& get_children(
'post_type=attachment&post_mime_type=image&post_parent='
. get_the_id() );
08
foreach
(
$images
as
$imageID
=>
$imagePost
)
09
$fullimage
= wp_get_attachment_image(
$imageID
,
$size
, false);
10
$imagedata
= wp_get_attachment_image_src(
$imageID
,
$size
, false);
11
$width
= (
$imagedata
[1]+2);
12
$height
= (
$imagedata
[2]+2);
15
add_shortcode(
"postimage"
,
"wpdx_postimage"
);
显示图片
2
echo
do_shortcode(
"[postimage]"
); ?>
16. 截取摘要
wordpress二次开发技巧之functions.php篇
02
function
new_excerpt_length(
$length
) {
05
add_filter(
'excerpt_length'
,
'new_excerpt_length'
);
08
function
new_excerpt_more(
$more
) {
11
add_filter(
'excerpt_more'
,
'new_excerpt_more'
);
17. 获取文章第一个分类目录
因为一篇文章可能属于多个目录,有些地方只需要输出一个
2
function
get_first_category(){
3
$category
= get_the_category();
8
add_filter(
'get_first_category'
,
'get_first_category'
);
HTML
18. 分类页获得文章简介
wordpress二次开发技巧之functions.php篇
2
function
get_category_expert(
$length
=240){
3
$content
= get_the_content();
7
add_filter(
'get_category_expert'
,
'get_category_expert'
);
19. 根据页面类型指定每页显示的文章数
02
function
custom_posts_per_page(
$query
){
04
$query
->set(
'posts_per_page'
,9);
07
$query
->set(
'posts_per_page'
,5);
10
$query
->set(
'posts_per_page'
,-1);
13
$query
->set(
'posts_per_page'
,4);
16
$query
->set(
'posts_per_page'
,9);
19
$query
->set(
'posts_per_page'
,-1);
23
add_action(
'pre_get_posts'
,
'custom_posts_per_page'
);
20. 缩略图
02
if
( function_exists(
'add_theme_support'
) )add_theme_support(
'post-thumbnails'
);
08
function
dm_the_thumbnail() {
14
if
( has_post_thumbnail() ) {
18
the_post_thumbnail(
'thumbnail'
);
24
$content
=
$post
->post_content;
26
preg_match_all(
'/"]?(.+?)['"
]?(?:(?: |t|r|n)+.*?)?>/sim',
$content
,
$strResult
, PREG_PATTERN_ORDER);
28
$n
=
count
(
$strResult
[1]);
32
$thumb_first
=
$strResult
[1][0];
33
$thumb_split
=
strrpos
(
$thumb_first
,
'.'
);
34
$thumb_before
=
substr
(
$thumb_first
, 0,
$thumb_split
);
35
$thumb_after
=
substr
(
$thumb_first
,
$thumb_split
,
strlen
(
$thumb_first
));
37
$thumb_before
= preg_replace(
$reg
,
""
,
$thumb_before
);
38
$thumb_src
=
$thumb_before
.
'-215x115'
.
$thumb_after
;
42
$random
= mt_rand(0, 19);
43
echo
'.get_permalink().
'" title="阅读全文"> .get_bloginfo(
'template_url'
).
'/images/thumbnail/default_thumbnail_'
.
$random
.
'.jpg" alt="缩略图" />'
;
显示缩略图
2
if
(has_post_thumbnail()){ the_post_thumbnail();} ?>