




免费预览已结束,剩余35页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Wordpress主题制作个人主题的开发记录。主要介绍函数的使用。创建主题在wp-content下面的themes文件夹中创建自己主题的文件夹。我的文件夹名称是zingson。在文件下面创建style.css文件与index.php文件,一个主题中,这两个文件是必须的,其它的一些文件后面再加。这两个文件建好之后,去后台就可以看到自己添加的主题,启用主题,打开首页能看到你index.php的内容了。style.css文件说明style.css文件中必须含有主题的注释头信息。以下是默认主题的style.css文件:/*Theme Name: Twenty TwelveTheme URI: /themes/twentytwelveAuthor: the WordPress teamAuthor URI: /Description: The 2012 theme for WordPress is a fully responsive theme that looks great on any device. Features include a front page template with its own widgets, an optional display font, styling for post formats on both index and single views, and an optional no-sidebar page template. Make it yours with a custom menu, header image, and background.Version: 1.3License: GNU General Public License v2 or laterLicense URI: /licenses/gpl-2.0.htmlTags: light, gray, white, one-column, two-columns, right-sidebar, flexible-width, custom-background, custom-header, custom-menu, editor-style, featured-images, flexible-header, full-width-template, microformats, post-formats, rtl-language-support, sticky-post, theme-options, translation-readyText Domain: twentytwelveThis theme, like WordPress, is licensed under the GPL.Use it to make something cool, have fun, and share what youve learned with others.*/上面内容是从默认主题复制过来的,大概可以看出什么意思了。注释说明:Theme Name: 这里填主题名称Theme URI: 这里填主题介绍的网址,没有就填你的博客网址吧Description: 这里填主题的简短介绍Version: 版本号Author: 作者名Author URI: 作者的网址Tags: 标签,多个用半角逗号隔开Index.phpWordPress主题模板文件的列表。用户的主题中也可能带有其它样式表单、图片或文件,不过下面这些文件在WordPress中都有着特殊意义。style.css:主样式表单。主题中必须包含style.css文件,而style.css文件中必须含有主题的注释头信息。index.php:主模板。如果用户使用的主题有自己的模板,必须具备index.php文件。Wordpress的实用函数工具函数bloginfo()这个函数定义在wp-includes/general-template.php文件。函数源代码:/*Displayinformationabouttheblog.*seeget_bloginfo()Forpossiblevaluesfortheparameter.*since0.71*paramstring$showWhattodisplay.*/function bloginfo( $show= ) echo get_bloginfo( $show, display );看到这里使用的是get_bloginfo()两个参数,这里就不进一步追进了。使用方法如下:变量$show是要显示的参数,包括以下字段属性:name = 站点名称description= 站点描述admin_email = 管理员邮箱url = 站点地址use home_url(/) insteadwpurl = http:/example/wp use site_url(/) insteadstylesheet_directory = 主题样式文件夹stylesheet_url = 主题样式路径template_directory = 主题文件夹template_url = 主题路径atom_url = http:/example/feed/atomrss2_url = http:/example/feedrss_url = http:/example/feed/rsspingback_url = http:/example/wp/xmlrpc.phprdf_url = http:/example/feed/rdfcomments_atom_url = http:/example/comments/feed/atomcomments_rss2_url = http:/example/comments/feedcharset = UTF-8html_type = text/htmllanguage = 语言text_direction = ltrversion = 版本函数是把内容输出,表现在页面上,它的作用就像ehco;如果想要赋值调用的话,我们需要使用Wordpress给我们提供的另一个函数get_bloginfo(),例如:网站优化:如果已经有了固定的域名,有了成型的网站,设计的主题是私有主题(即设置之初就没有打算共享),基于以上几个条件,bloginfo函数用处似乎不大。使用实例:下面代码在index.php中。主题首页bloginfo获取站点名称:bloginfo获取站点描述:-get_bloginfo()这个函数上面见过了。下面贴源代码出来:(代码看不下去可以看后面的解释)这个函数定义在wp-includes/general-template.php 文件。functionget_bloginfo( $show = , $filter = raw ) switch( $show ) casehome : / DEPRECATEDcasesiteurl : / DEPRECATED_deprecated_argument( _FUNCTION_, 2.2, sprintf( _(The %s option is deprecated for the family of bloginfo() functions. ), $show ) . . sprintf( _( Use the %s option instead. ), url ) );caseurl :$output = home_url();break;casewpurl :$output = site_url();break;casedescription:$output = get_option(blogdescription);break;caserdf_url:$output = get_feed_link(rdf);break;caserss_url:$output = get_feed_link(rss);break;caserss2_url:$output = get_feed_link(rss2);break;caseatom_url:$output = get_feed_link(atom);break;casecomments_atom_url:$output = get_feed_link(comments_atom);break;casecomments_rss2_url:$output = get_feed_link(comments_rss2);break;casepingback_url:$output = site_url( xmlrpc.php );break;casestylesheet_url:$output = get_stylesheet_uri();break;casestylesheet_directory:$output = get_stylesheet_directory_uri();break;casetemplate_directory:casetemplate_url:$output = get_template_directory_uri();break;caseadmin_email:$output = get_option(admin_email);break;casecharset:$output = get_option(blog_charset);if ( = $output) $output = UTF-8;break;casehtml_type :$output = get_option(html_type);break;caseversion:global$wp_version;$output = $wp_version;break;caselanguage:$output = get_locale();$output = str_replace(_, -, $output);break;casetext_direction:/_deprecated_argument( _FUNCTION_, 2.2, sprintf( _(The %s option is deprecated for the family of bloginfo() functions. ), $show ) . . sprintf( _( Use the %s function instead. ), is_rtl() ) );if ( function_exists( is_rtl ) ) $output = is_rtl() ? rtl : ltr; else $output = ltr;break;casename:default:$output = get_option(blogname);break;$url = true;if (strpos($show, url) = false&strpos($show, directory) = false&strpos($show, home) = false)$url = false;if ( display = $filter ) if ( $url )$output = apply_filters(bloginfo_url, $output, $show);else$output = apply_filters(bloginfo, $output, $show);return$output;可以看出有连个参数get_bloginfo( $show = , $filter = raw ),$show对应哪些值从里面的switch可以看到有哪些参数可以使用!在bloginfo()的介绍里面有参数的详细说明。get_option()获取options表key对应的值get_option(参数)这个函数通过数据库wp_options表中的key获取value 的值。Wp_是表前缀,根据自己的表定。上面的get_bloginfo()介绍中有使用到这个函数获取值。可以回过头去看看。使用实例:这个能直接获取wp_options中的列option_name值为blogdescription的博客描述这个函数对应的有add_option()就是添加了。get_template_directory_uri()主题目录检索当前主题的模板目录的URI:script src=/js/html5.js type=text/javascript这个没说什么说的了。数据库操作函数(能直接添加删除查询的)wp_insert_post()插入一篇文章源代码,自己去搜这个方法吧,只有连个参数,但第一个是数组,内容有点多。functionwp_insert_post( $postarr, $wp_error = false )调用wp_insert_post()前需创建对象以传递组成文章的必要元素。wp_insert_post()可自动填写默认表格,但用户需提供文章标题和内容,否则数据库写入不成功。用户可在数据库中简单定义新关键字,之后就可以添加更多文章要素。关键字应与数据库wp_posts表格中纵列名称相匹配。/ Create post object $my_post = array(); $my_postpost_title = My post; $my_postpost_content = This is my post.; $my_postpost_status = publish; $my_postpost_author = 1; $my_postpost_category = array(8,39); / Insert the post into the database wp_insert_post( $my_post ); 上面提到的默认表格在函数主体中有所定义。定义如下:$defaults = array( post_status = draft, post_type = post, post_author = $user_ID, ping_status = get_option(default_ping_status), post_parent = 0, menu_order = 0, to_ping = , pinged = , post_password = , guid = , post_content_filtered = , post_excerpt = ); 类别需要将类别作为整数数组传递,该数组应与数据库中的类别编号相匹配。即使文章只属于某一项类别,情况也应如此。参数$post(对象)(必需)能表示可组成文章元素的对象。这些元素与数据库wp_posts表格中的纵列名称应一一对应。默认值:空文章数组的内容可取决于用户的默认值的信赖程度。下表列出了用户可为文章设置的所有选项:$post = array( comment_status = closed | open / closed means no comments. ID = /Are you updating an existing post? menu_order = /If new post is a page, sets the order should it appear in the tabs. page_template = /Sets the template for the page. ping_status =? /Ping status? pinged = ? /? post_author = /The user ID number of the author. post_category = array(, ) /Add some categories. post_content = /The full text of the post. post_date = Y-m-d H:i:s /The time post was made. post_date_gmt = Y-m-d H:i:s /The time post was made, in GMT. post_excerpt = /For all your post excerpt needs. post_parent = /Sets the parent of the new post. post_password =? /password for post? post_status = draft | publish | pending /Set the status of the new post. post_title = /The title of your post. post_type = post | page /Sometimes you want to post a page. tags_input = , , /For tags. to_ping =? /? ); 返回的值若文章成功加入数据库,返回文章编号。否则返回0.与上面对应的是修改了,其实都一样:function wp_update_post( $postarr = array(), $wp_error = false )get_post_meta() 查询postmeta数据三个参数很明显了。文章ID,key,$single是否是单一的,意思是key对应的值是否只有一个。False的时候返回的是数组,true的时候返回字符串。functionget_post_meta($post_id, $key = , $single = false) return get_metadata(post, $post_id, $key, $single);add_post_meta() 向postmeta表插入数据源代码:Functionadd_post_meta($post_id, $meta_key, $meta_value, $unique = false)不解释,很明白了。update_post_meta修改postmeta表修改update_post_meta($post_id, $meta_key, $meta_value, $unique = false)显示文章的函数在这个文件中定义了很多的函数:wp-includes/post-template.php看这个文件的时候进入wp-includes/post.php的get_posts()方法,在执行the_content等方法的时候需要先执行get_post();通过这个文件里面的函数可以获取wp_posts表中的需要的字段值,下面不一一列出来了。貌似也没啥意思,找出这个一看就知道了.本人不太喜欢这一块儿内容,都是自己写的sql查询实现功能的,以后有时间再回来补充吧。这里就没有做详细介绍了。get_posts查询文章列表源代码:functionget_posts($args = null) $defaults = array(numberposts = 5, offset = 0,category = 0, orderby =post_date,order =DESC, include =array(),exclude =array(), meta_key =,meta_value =, post_type =post,suppress_filters =true);$r = wp_parse_args( $args, $defaults );if ( empty( $rpost_status ) )$rpost_status = ( attachment = $rpost_type ) ? inherit : publish;if ( ! empty($rnumberposts) &empty($rposts_per_page) )$rposts_per_page = $rnumberposts;if ( ! empty($rcategory) )$rcat = $rcategory;if ( ! empty($rinclude) ) $incposts = wp_parse_id_list( $rinclude );$rposts_per_page = count($incposts); / only the number of posts included$rpost_in = $incposts; elseif ( ! empty($rexclude) )$rpost_not_in = wp_parse_id_list( $rexclude );$rignore_sticky_posts = true;$rno_found_rows = true;$get_posts = new WP_Query;return$get_posts-query($r);对参数$args的说明: 10, /需要提取的文章数offset = 0,/以第几篇文章为起始位置category = ,/分类的ID,多个用逗号将分类编号隔开,或传递编号数组。orderby = post_date,/排序规则(注1)order = DESC,/升序、降序 ASC 升序(低到高) DESC 降序(高到底)include = ,/要显示文章的IDexclude = ,/要排除文章的IDmeta_key = ,/自定义字段名称meta_value = ,/自定义字段的值,配合上一个参数,来选择显示符合自定义字段数值的文章。/post(日志)默认,page(页面),/attachment(附件),any (所有)post_type = post,post_mime_type = ,/文章的 mime 类型post_parent = ,/要显示文章的父级 IDpost_status = publish );/文章状态?the_content()获取文章内容显示文章内容,全部function the_content( $more_link_text = null, $strip_teaser = false)get_the_content也是获取文章内容存在more的情况,显示一个查看更多内容function get_the_content( $more_link_text = null, $strip_teaser = false )is_sticky() 判断文章置顶看源代码:functionis_sticky( $post_id = 0 ) $post_id = absint( $post_id );if ( ! $post_id )$post_id = get_the_ID();$stickies = get_option( sticky_posts );if ( ! is_array( $stickies ) )returnfalse;if ( in_array( $post_id, $stickies ) )returntrue;returnfalse;可以看出是通过文章ID来判断当前文章是否是置顶文章。测试代码:id) echo置顶-; ?附加说明:get_option(sticky_posts )从这句代码,我在数据库options表中通过key为sticky_posts看到这样的一个value的字符串:a:2:i:0;i:81;i:1;i:94;这里面的81与94是我两个置顶的文章。找了半天,终于找到wordpress置顶的标识写哪儿了就是这个sticky_posts对应的value值了。侧边栏函数底部函数其它函数wp_title()显示所有领域的博客或检索页面标题。代码:这个函数定义在wp-includes/general-template.php 文件。function wp_title($sep = », $display = true, $seplocation = )可以看出需要三个参数。body_class()主要用于输出一个个性化的唯一的body class的值,当然你也可以手工加入一些特定的值。body_class函数在WordPress 中主要用来输出body的class值,而这个值在不同的页面会显示的不同。通过控制不同class的样式,可以让你的博客炫丽起来。post_class()同上,以后在补充。home_url()见底部的“获取项目目录部分”esc_url()主要用于 URL 过滤:1. 拒绝不是下面协议的 URL (defaulting to http, https, ftp, ftps, mailto, news, irc, gopher, nntp, feed, and telnet)2. 消除无效字符和删除危险字符。3. 将字符转换成 HTML 实体,并且将&和单引号()转换成数字实体: ?参数说明:$url(string) (required) 将要被清理过滤的 URLDefault: 无$protocols(array) (optional) 可以接受协议的数组,如果没有设置,可以是:http, https, ftp, ftps, mailto, news, irc, gopher, nntp, feed, telnet。Default: 无$_context(string) (optional) 如何返回 URL。Default: display返回值:(string)已经清理过滤的 URLesc_attr()将& (小于号,大于号,&,双引号,单引号)编码,转成HTML 实体,已经是实体的并不转换。在转义 HTML 属性的时候经常用这个函数(特别是表单的值,比如alt, value, title等等)转义并输出值,使用esc_attr_e代替。用法:返回值:已经编码成 HTML 实体的文本,就是html标签的字符串。实例:转换value的值input type=text name=fname value=esc_attr_e()上面介绍的esc_attr是返回值,而esc_attr_e()是直接输出。_e()之后补充。wp_nav_menu()头部菜单,很关键的一个函数,看源代码:wp-includes/nav-menu-template.php文件下:functionwp_nav_menu( $args = array() ) static$menu_id_slugs = array();$defaults = array( menu =, container =div, container_class =, container_id =, menu_class =menu, menu_id =,echo =true, fallback_cb =wp_page_menu, before =, after =, link_before =, link_after =, items_wrap =%3$s,depth = 0, walker =, theme_location = );后面的省略了,比较多。咱就看这一部分可以看出有一个参数array().并且有一个默认的值,就是不写参数的时候,使用的是默认值。看一个很简单的例子:这样输出的结果,html标签如下:首页网站模版网站建设技术探讨有点乱,但是应该看的清楚,这是默认的生成结果,我们可以通过参数改变它的标签与id级class的值来改变样式,定义自己喜欢的样式。如下面的配置例子:, /指定显示的导航名,如果没有设置,则显示第一个menu =header-menu,container =div, /最外层容器标签名可以是nav div等container_class =primary, /最外层容器class名container_id =primaryid,/最外层容器id值menu_class =sf-menu, /ul标签classmenu_id =topnav,/ul标签idecho =true,/是否打印,默认是true,如果想将导航的代码作为赋值使用,可设置为falsefallback_cb =wp_page_menu,/备用的导航菜单函数,用于没有在后台设置导航时调用before =,/显示在导航a标签之前after =,/显示在导航a标签之后link_before =,/显示在导航链接名之后link_after =,/显示在导航链接名之前items_wrap =%3$s, /菜单depth = 0, /显示的菜单层数,默认0,0是显示所有层walker =/调用一个对象定义显示导航菜单);?注意:需要在functions.php中加入下面这行代码才能在后台编辑你的菜单。也可以叫启用菜单管理,有些主题是不支持改变菜单的。就是没有加这个。/显示菜单register_nav_menus(array(header-menu = _( 自定义菜单 );/显示自定义背景add_theme_support( custom-background, array(default-color =e6e6e6,);加上之后,后台才会有菜单与背景的这个选项。header_image()感觉这个没什么用,后续再补充。get_header_image()感觉这个没什么用,后续再补充。get_custom_header()wordpress默认的一个主题里面的头部,不用管,没什么用。咱开发自己的。header部分函数列表bloginfo()get_bloginfo()wp_title()get_template_directory_uri()body_class()home_url()esc_url()esc_attr()_e()esc_attr_e()wp_nav_menu()get_header_image()header_image()get_custom_header()函数说明总结通过上面的分析,应该能做出一个主题的头部部分了吧。Wordpress帮我们定义了大量的函数,可以直接使用它,快速开发自己的漂亮的主题。这里只介绍头部的一小部分,下面还会继续介绍,更多内容可以查看源代码及官网的API,API都是英文的,有点烦,所以写这个给自己做一下笔记。content部分the_ID():返回博文 ID;the_permalink():返回博文固定链接 URL;the_title():返回博文标题;the_time(M):返回发表日期中的月份;the_time(d):返回发表日期中的天;the_author():返回博文作者;the_category():返回博文的类别;the_content():返回博文的内容,其中的参数表示用于“更多内容”的链接文本;函数说明have_poststhe_titlethe_postget_template
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学食堂工作年度总结及改进方案
- 儿童加减法的讲解
- 景区接待流程方案范本
- 食堂水池使用方案范本
- 电力安全技术等级题库及答案解析
- 导尿管及造瘘管的护理
- 多彩的课间活动教学课件
- 常州老小区外墙施工方案
- 美团电商年终个人工作总结
- 东软云医院管理信息系统
- 医学实验室生物安全培训培训课件
- 高考语文文学类阅读复习备考:革命题材小说阅读题 专项练习 (含答案解析)
- 西湖十景-美术优质课课件
- 海绵城市总结课件
- 会计学全套课件第一学期公开课一等奖省优质课大赛获奖课件
- 光伏站电力监控系统介绍参考课件
- 公开课第一课素描基础入门课件
- 果蔬加工工艺学:果蔬汁
- 门机防腐施工方案
- 定向井井眼轨迹计算课件
- 石景山区语文一模试卷讲评分析
评论
0/150
提交评论