您的位置 首页 php

WordPress常用的函数、方法汇总

选择使用 WordPress来搭建博客,主要原因便在于 WordPress有较高的流行度,还有各种围绕其进行的开发的扩展功能应有尽有,基本上可以说我们在博客建设中所需的任何功能,都已有人想到并得到实现,让你不必在博客具体的技术实现及功能扩展方面投入过多的精力,而更专注于内容建设。

本文以技术手册的方式简单汇总WordPress主题模板的基本资料,希望能对WordPress主题模板开发的朋友解渴。

WordPress 主题模板基本文件

一套完整的 WordPress 主题模板应至少具有如下文件:,WordPress基本模板文件,WordPress模板必备文件:

style.css : CSS(样式表)文件index.php : 主页模板archive.php : Archive/Category模板404.php : Not Found 错误页模板comments.php : 留言/回复模板footer.php : Footer模板header.php : Header模板sidebar.php : 侧栏模板page.php : 内容页(Page)模板single.php : 内容页(Post)模板searchform.php : 搜索表单模板search.php : 搜索结果模板

基本条件判断Tag

is_home() : 是否为主页is_single() : 是否为内容页(Post)is_page() : 是否为内容页(Page)is_category() : 是否为Category/Archive页is_tag() : 是否为Tag存档页s_date() : 是否为指定日期存档页is_year() : 是否为指定年份存档页is_month() : 是否为指定月份存档页is_day() : 是否为指定日存档页is_time() : 是否为指定时间存档页is_archive() : 是否为存档页is_search() : 是否为搜索结果页s_404() : 是否为 “HTTP 404: Not Found” 错误页is_paged() : 主页/Category/Archive页是否以多页显示

Header部分常用到的PHP函数

<?php bloginfo (’name’); ?> : 博客名称(Title)<?php bloginfo (’stylesheet_url’); ?> : CSS文件路径<?php bloginfo (’pingback_url’); ?> : PingBack Url<?php bloginfo (’template_url’); ?> : 模板文件路径<?php bloginfo (’version’); ?> : WordPress版本<?php bloginfo (’atom_url’); ?> : Atom Url<?php bloginfo (’rss2_url’); ?> : RSS 2.o Url<?php bloginfo (’url’); ?> : 博客 Url<?php bloginfo (’html_type’); ?> : 博客网页Html类型<?php bloginfo (’charset’); ?> : 博客网页编码<?php bloginfo (’description’); ?> : 博客描述<?php wp_title(); ?> : 特定内容页(Post/Page)的标题

模板常用的PHP函数及命令

<?php get_header(); ?> : 调用Header模板<?php get_sidebar(); ?> : 调用Sidebar模板<?php get_footer(); ?> : 调用Footer模板<?php the_content(); ?> : 显示内容(Post/Page)<?php the_excerpt(); ?>: 显示摘要<?php if (have_posts()) : ?> : 检查是否存在Post/Page<?php while (have_posts()) : the_post(); ?> : 如果存在Post/Page则予以显示<?php endwhile ; ?> : While 结束<?php endif ; ?> : If 结束<?php the_time(’字符串’) ?> : 显示时间,时间格式由“字符串”参数决定,具体参考PHP手册<?php comments_popup_link(); ?> : 正文中的留言链接。如果使用 comments_popup_script() ,则留言会在新窗口中打开,反之,则在当前窗口打开<?php the_title(); ?> : 内容页(Post/Page)标题<?php the_permalink() ?> : 内容页(Post/Page) Url<?php the_category(’, ‘) ?> : 特定内容页(Post/Page)所属Category<?php the_author(); ?> : 作者<?php the_ID(); ?> : 特定内容页(Post/Page) ID<?php edit_post_link(); ?> : 如果用户已登录并具有权限,显示编辑链接<?php get_links_list(); ?> : 显示Blogroll中的链接<?php comments_template(); ?> : 调用留言/回复模板<?php wp_list_pages(); ?> : 显示Page列表<?php wp_list_categories(); ?> : 显示Categories列表<?php next_post_link(’ %link ‘); ?> : 下一篇文章链接<?php previous_post_link(’%link’); ?> : 上一篇文章链接<?php get_calendar(); ?> : 日历<?php wp_get_archives() ?> : 显示内容存档<?php posts_nav_link(); ?> : 导航,显示上一篇/下一篇文章链接<?php include (TEMPLATEPATH . ‘/文件名’); ?> : 嵌入其他文件,可为定制的模板或其他类型文件

与模板相关的其他函数

<?php _e(’Message’); ?> : 输出相应信息<?php wp_register(); ?> : 显示注册链接<?php wp_loginout(); ?> : 显示登录/注销链接<–next page–> : 将当前内容分页<–more–> : 将当前内容截断,以不在主页/目录页显示全部内容<?php timer_stop(1); ?> : 网页加载时间(秒)<?php echo get_num_queries(); ?> : 网页加载查询量

其它

<?php get_archives(’postbypost’, 10); ?> 调用最近的10篇日志<?php$rand_posts = get_posts(’numberposts=10&orderby=rand’);foreach ( $rand_posts as $post ) :?><li><a href= "”<?php" the_permalink();= "" ?= "" >”><?php the_title(); ?></a></li><?php endforeach ; ?>

随机调用10篇日志

<?php the_tags(’Post Tags :’, ‘, ‘, ‘<br />’); ?> 调用标签<?php if (get_the_tags()) the_tags(’Tags:’,’,’,’ end ’); ?>

标签存在时调用

<?php wp_tag_cloud(’smallest=1&largest=9&’); ?> 调用标签云<?php $posts = get_posts( “category=12&numberposts=4″ ); ?> 获得4篇12分类中的日志

再来一些更详细的判断函数

<?php if (is_single()):?>//这里写你想显示的内容,包括函数<?php endif ;?>

下面是直接来自wordpress.org

is_single() 判断是否是具体文章的页面is_single(’17′) 判断是否是具体文章(id=17)的页面is_single(’Beef Stew’) 判断是否是具体文章(标题判断)的页面is_single(’beef-stew’) 判断是否是具体文章(slug判断)的页面comments_open() 是否留言开启pings_open() 是否开启pingis_page() 是否是页面is_page(’42′) 同single,id判断,即是否是id为42的页面is_page(’About Me’) title判断is_page(’about-me’) slug判断is_category() 是否是分类is_category(’6′) 同single,id判断,即是否是id为6的分类is_category(’Cheeses’) title判断is_category(’cheeses’) slug判断in_category(’5′) 判断当前的文章是否属于分类5is_author() 将所有的作者的页面显示出来is_author(’1337′) 显示author number为1337的页面is_author(’Elite Hacker’) 通过昵称来显示当前作者的页面is_author(’elite-hacker’)is_date()is_year()is_month()is_day()is_time()is_archive() 判断当前是否是归档页面is_search() 判断是否是搜索is_404() 判断页面是否404is_paged() 判断是否翻页,比如你当前的blog是http: //domain.com,显示http: //domain.com?paged=2的时候,这个判断将返回真,通过这个函数可以配合is_home来控制某些只能在首页显示的界面,例如: if (is_home() && !is_paged()

以上就是WordPress常用的函数、方法汇总的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:WordPress常用的函数、方法汇总

文章地址:https://www.zhihuclub.com/4691.shtml

关于作者: 智云科技

热门文章

网站地图