wordpress二次开发中对于functions.php的制作是非常多的,今天我们来分享关于wordpress二次开发技巧之functions.php的制作,希望大家可以喜欢!
下面根据需求,对各种能实现进行简单介绍。
先对functions.php文件进行介绍,通过代码实现各功能。
1. widgets sidebar 侧边栏小工具
wordpress二次开发技巧之functions.php篇
02 |
if ( function_exists( 'register_sidebar' <span class = "crayon-sy" >) ) {
|
03 |
register_sidebar( array (
|
04 |
'name' <span class = "crayon-o" >=> 'First_sidebar' <span class = "crayon-sy" >,
|
05 |
'before_widget' <span class = "crayon-o" >=> '' <span class = "crayon-sy" >,
|
06 |
'after_widget' <span class = "crayon-o" >=> '' <span class = "crayon-sy" >,
|
07 |
'before_title' <span class = "crayon-o" >=> '<h4>' <span class = "crayon-sy" >,
|
08 |
'after_title' <span class = "crayon-o" >=> '</h4>'
|
10 |
register_sidebar( array (
|
11 |
'name' <span class = "crayon-o" >=> 'Second_sidebar' <span class = "crayon-sy" >,
|
12 |
'before_widget' <span class = "crayon-o" >=> '' <span class = "crayon-sy" >,
|
13 |
'after_widget' <span class = "crayon-o" >=> '' <span class = "crayon-sy" >,
|
14 |
'before_title' <span class = "crayon-o" >=> '<h4>' <span class = "crayon-sy" >,
|
15 |
'after_title' <span class = "crayon-o" >=> '</h4>'
|
17 |
register_sidebar( array (
|
18 |
'name' <span class = "crayon-o" >=> 'Third_sidebar' <span class = "crayon-sy" >,
|
19 |
'before_widget' <span class = "crayon-o" >=> '' <span class = "crayon-sy" >,
|
20 |
'after_widget' <span class = "crayon-o" >=> '' <span class = "crayon-sy" >,
|
21 |
'before_title' <span class = "crayon-o" >=> '<h4>' <span class = "crayon-sy" >,
|
22 |
'after_title' <span class = "crayon-o" >=> '</h4>'
|
24 |
register_sidebar( array (
|
25 |
'name' <span class = "crayon-o" >=> 'Fourth_sidebar' <span class = "crayon-sy" >,
|
26 |
'before_widget' <span class = "crayon-o" >=> '' <span class = "crayon-sy" >,
|
27 |
'after_widget' <span class = "crayon-o" >=> '' <span class = "crayon-sy" >,
|
28 |
'before_title' <span class = "crayon-o" >=> '<h4>' <span class = "crayon-sy" >,
|
29 |
'after_title' <span class = "crayon-o" >=> '</h4>'
|
33 |
register_nav_menus( array ( "primary" => "Primary Navigation" ));
|
2. 后台支持自定义菜单
wordpress二次开发技巧之functions.php篇
02 |
if (function_exists( 'register_nav_menus' <span class = "crayon-sy" >)){
|
05 |
'header-menu' <span class = "crayon-o" >=> __( '导航自定义菜单' <span class = "crayon-sy" >),
|
06 |
'footer-menu' <span class = "crayon-o" >=> __( '页角自定义菜单' <span class = "crayon-sy" >),
|
07 |
'sider-menu' <span class = "crayon-o" >=> __( '侧边栏菜单' <span class = "crayon-sy" >),
|
08 |
'primary' <span class = "crayon-o" >=> __( 'Primary Navigation' <span class = "crayon-sy" >, 'lee' <span class = "crayon-sy" >),
|
输出菜单,在如header.php插入显示菜单的代码
1 |
<?php wp_nav_menu( array ( 'container' <span class = "crayon-o" >=> '' <span class = "crayon-sy" >) ); ?>
|
3. 面包屑导航
02 |
function wheatv_breadcrumbs() {
|
03 |
$delimiter = '<i>></i>' <span class = "crayon-sy" >;
|
04 |
$name = '首页' <span class = "crayon-sy" >;
|
05 |
$currentBefore = '' <span class = "crayon-sy" >;
|
06 |
$currentAfter = '' <span class = "crayon-sy" >;
|
08 |
if ( !is_home() && !is_front_page() || is_paged() ) {
|
10 |
echo '' <span class = "crayon-sy" >;
|
13 |
$home = get_option( 'home' <span class = "crayon-sy" >);
|
14 |
echo '<a href="' <span class ="crayon-sy ">.$home.'" >'<span class = "crayon-sy" >. $name . ' </a>' <span class = "crayon-sy" >. $delimiter . ' ' <span 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, ' ' <span class = "crayon-sy" >. $delimiter . ' ' <span class = "crayon-sy" >));
|
23 |
echo $currentBefore . '' <span class = "crayon-sy" >;
|
25 |
echo '' <span class = "crayon-sy" >. $currentAfter ;
|
27 |
} elseif ( is_day() ) {
|
28 |
echo '' <span class = "crayon-sy" >. get_the_time( 'Y' <span class = "crayon-sy" >) . ' ' <span class = "crayon-sy" >. $delimiter . ' ' <span class = "crayon-sy" >;
|
29 |
echo '' <span class = "crayon-sy" >. get_the_time( 'F' <span class = "crayon-sy" >) . ' ' <span class = "crayon-sy" >. $delimiter . ' ' <span class = "crayon-sy" >;
|
30 |
echo $currentBefore . get_the_time( 'd' <span class = "crayon-sy" >) . $currentAfter ;
|
32 |
} elseif ( is_month() ) {
|
33 |
echo '' <span class = "crayon-sy" >. get_the_time( 'Y' <span class = "crayon-sy" >) . ' ' <span class = "crayon-sy" >. $delimiter . ' ' <span class = "crayon-sy" >;
|
34 |
echo $currentBefore . get_the_time( 'F' <span class = "crayon-sy" >) . $currentAfter ;
|
36 |
} elseif ( is_year() ) {
|
37 |
echo $currentBefore . get_the_time( 'Y' <span class = "crayon-sy" >) . $currentAfter ;
|
39 |
} elseif ( is_single() ) {
|
40 |
$cat = get_the_category(); $cat = $cat [0];
|
41 |
echo get_category_parents( $cat , TRUE, ' ' <span class = "crayon-sy" >. $delimiter . ' ' <span 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 [] = '' <span class = "crayon-sy" >. get_the_title( $page ->ID) . '' <span class = "crayon-sy" >;
|
57 |
$parent_id = $page ->post_parent;
|
59 |
$breadcrumbs = array_reverse ( $breadcrumbs );
|
60 |
foreach ( $breadcrumbs as $crumb ) echo $crumb . ' ' <span class = "crayon-sy" >. $delimiter . ' ' <span class = "crayon-sy" >;
|
65 |
} elseif ( is_search() ) {
|
66 |
echo $currentBefore . '搜索结果' <span class = "crayon-sy" >. get_search_query() . '' <span class = "crayon-sy" >. $currentAfter ;
|
68 |
} elseif ( is_tag() ) {
|
69 |
echo $currentBefore . '搜索标签: ' <span class = "crayon-sy" >;
|
71 |
echo '' <span class = "crayon-sy" >. $currentAfter ;
|
73 |
} elseif ( is_author() ) {
|
75 |
$userdata = get_userdata( $author );
|
76 |
echo $currentBefore . 'Articles posted by ' <span class = "crayon-sy" >. $userdata ->display_name . $currentAfter ;
|
78 |
} elseif ( is_404() ) {
|
79 |
echo $currentBefore . 'Error 404' <span class = "crayon-sy" >. $currentAfter ;
|
82 |
if ( get_query_var( 'paged' <span class = "crayon-sy" >) ) {
|
83 |
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (' <span class = "crayon-sy" >;
|
84 |
echo __( '第' <span class = "crayon-sy" >) . '' <span class = "crayon-sy" >. get_query_var( 'paged' <span class = "crayon-sy" >) . '页' <span class = "crayon-sy" >;
|
85 |
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')' <span class = "crayon-sy" >;
|
87 |
echo '' <span class = "crayon-sy" >;
|
显示面包屑导航(category.php或single.php等)
1 |
<?php wheatv_breadcrumbs(); ?> |
4. 文章访问量(点击数)
wordpress二次开发技巧之functions.php篇
02 |
function getPostViews( $postID ){
|
03 |
$count_key = 'post_views_count' <span class = "crayon-sy" >;
|
04 |
$count = get_post_meta( $postID , $count_key , true);
|
05 |
if ( $count == '' <span class = "crayon-sy" >){
|
06 |
delete_post_meta( $postID , $count_key );
|
07 |
add_post_meta( $postID , $count_key , '0' <span class = "crayon-sy" >);
|
-
rust-bitcoin/rust-bitcoin: Rust Bitcoin library
阅读:1051|2022-08-15
-
bradtraversy/iweather: Ionic 3 mobile weather app
阅读:1565|2022-08-30
-
joaomh/curso-de-matlab
阅读:1138|2022-08-17
-
魔兽世界怀旧服已经开启两个多月了,但作为一个猎人玩家,抓到“断牙”,已经成为了一
阅读:994|2022-11-06
-
rugk/mastodon-simplified-federation: Simplifies following and interacting with r
阅读:1067|2022-08-17
-
Delphi取整函数round、trunc、ceil和floor1.Round(四舍六入五留双)功能说明:对一个
阅读:1462|2022-07-18
-
1.histc函数,用于统计范围内数量,可计算概率密度
histc函数制定数值边界为分界条件
阅读:539|2022-07-18
-
Tangshitao/Dense-Scene-Matching: Learning Camera Localization via Dense Scene Ma
阅读:748|2022-08-16
-
相信不少果粉在对自己的设备进行某些操作时,都会碰到Respring,但这个 Respring 到底
阅读:357|2022-11-06
-
lightningtgc/MProgress.js: Material Progress —Google Material Design Progress l
阅读:395|2022-08-17
|
请发表评论