• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

wordpress二次开发技巧之functions.php篇

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

wordpress二次开发中对于functions.php的制作是非常多的,今天我们来分享关于wordpress二次开发技巧之functions.php的制作,希望大家可以喜欢!

下面根据需求,对各种能实现进行简单介绍。

先对functions.php文件进行介绍,通过代码实现各功能。

1. widgets sidebar 侧边栏小工具

wordpress二次开发技巧之functions.php篇

01 /** widgets sidebar 侧边栏小工具*/
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>'
09   ));
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>'
16   ));
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>'
23   ));
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>'
30   ));
31 }
32   
33 register_nav_menus(array("primary" => "Primary Navigation"));

2. 后台支持自定义菜单

wordpress二次开发技巧之functions.php篇

01 /*nav  后台自定义菜单*/
02 if(function_exists('register_nav_menus'<span class="crayon-sy">)){
03   register_nav_menus(
04     array(
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">),
09     )
10   );
11 }

输出菜单,在如header.php插入显示菜单的代码

1 <?php wp_nav_menu( array'container' <span class="crayon-o">=> '' <span class="crayon-sy">) ); ?>
 

3. 面包屑导航

01 // 面包屑导航注册代码
02 function wheatv_breadcrumbs() {
03   $delimiter '<i>></i>'<span class="crayon-sy">;
04   $name '首页'<span class="crayon-sy">; //text for the 'Home' link
05   $currentBefore ''<span class="crayon-sy">;
06   $currentAfter ''<span class="crayon-sy">;
07   
08   if ( !is_home() && !is_front_page() || is_paged() ) {
09   
10   echo ''<span class="crayon-sy">;
11   global $post;
12   // $home = get_bloginfo('url');
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">;
15   
16   if ( is_category() ) {
17     global $wp_query;
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">;
24       single_cat_title();
25       echo '' <span class="crayon-sy">. $currentAfter;
26   
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;
31   
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;
35   
36     elseif ( is_year() ) {
37       echo $currentBefore . get_the_time('Y'<span class="crayon-sy">) . $currentAfter;
38   
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">);
42       echo $currentBefore;
43       the_title();
44       echo $currentAfter;
45   
46     elseif ( is_page() && !$post->post_parent ) {
47       echo $currentBefore;
48       the_title();
49       echo $currentAfter;
50   
51     elseif ( is_page() && $post->post_parent ) {
52       $parent_id $post->post_parent;
53       $breadcrumbs array();
54       while ($parent_id) {
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;
58       }
59       $breadcrumbs array_reverse($breadcrumbs);
60       foreach ($breadcrumbs as $crumbecho $crumb ' ' <span class="crayon-sy">.$delimiter ' '<span class="crayon-sy">;
61       echo $currentBefore;
62       the_title();
63       echo $currentAfter;
64   
65     elseif ( is_search() ) {
66       echo $currentBefore '搜索结果' <span class="crayon-sy">. get_search_query() . '' <span class="crayon-sy">. $currentAfter;
67   
68     elseif ( is_tag() ) {
69       echo $currentBefore '搜索标签: '<span class="crayon-sy">;
70       single_tag_title();
71       echo '' <span class="crayon-sy">. $currentAfter;
72   
73     elseif ( is_author() ) {
74       global $author;
75       $userdata = get_userdata($author);
76       echo $currentBefore 'Articles posted by ' <span class="crayon-sy">. $userdata->display_name . $currentAfter;
77   
78     elseif ( is_404() ) {
79       echo $currentBefore 'Error 404' <span class="crayon-sy">. $currentAfter;
80   }
81   
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">;
86   }
87   echo ''<span class="crayon-sy">;
88   }
89 }
 

显示面包屑导航(category.php或single.php等)

1 <?php wheatv_breadcrumbs(); ?>
 

4.  文章访问量(点击数)

wordpress二次开发技巧之functions.php篇

01 //文章点击数
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">);
08         return "0";
09     }
10     return $count;
11 }<

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
python+djangoMTV框架和phpMVC框架发布时间:2022-07-10
下一篇:
php的echo和return的区别发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap