Worpress常用函数

Wordpress Alan 9年前 (2015-10-15) 5418次浏览 0个评论 扫描二维码

为方便Wordpress的二次开发的常见修改,将在本帖中整理一些经常使用的Wordpress函数,以便查找。

主要文件和目录

index.php    首页

header.php    头部

footer.php    底部

sidebar.php    侧边栏

category.php    目录页

single.php   文章页

functions.php    主题自定义函数

style.css    主css文件

Action和Filter相关

add_action(‘alan’, ‘functionA’)    将函数functionA挂载到动作alan上,还有优先级等可选参数

do_action(‘alan’)    执行挂载在动作alan上的所有函数

WordPress内置的Action请参阅:http://codex.wordpress.org/Action_Reference

add_filter(‘alan’, ‘functionA’)   将函数functionA挂载到过滤器alan上,还有优先级等可选参数

apply_filters(‘alan’, $arg)    将$arg传入挂载在动作alan上的所有函数并执行

WordPress内置的Filter请参阅:http://codex.wordpress.org/Plugin_API/Filter_Reference

首页分类相关

bloginfo()    获取博客相关信息

get_header()

get_sidebar()    调用默认侧边栏的内容或sidebar.php的内容

register_sidebar()    用于在functions.php中注册小工具,在后台外观>小工具中进行编辑,并通过dynamic_sidebar()函数调用

get_footer()

get_template_part()

the_archive_title()

posts_nav_link()    输出页码,可进行上下页翻页

wp_list_cats()    获取分类列表

single_cat_title()    获取当前分类名称

数据库相关

get_option()    从options表中取出数据

update_option()    向options表中插入数据

$wpdb->insert()    向指定表插入一条数据

$wpdb->insert_id    获取最新插入行的id

$wpdb->prefix    获取数据表前缀

$wpdb->update()    更新指定数据表中满足指定条件的记录

$wpdb->get_var()    通过传入SQL语句来获取指定的数据

$wpdb->query()    执行原生的SQL语句(DELETE, UPDATE)

$wpdb->get_results()    获取SELECT语句查询结果

$wpdb->get_row()    获取一行SELECT语句查询结果

$wpdb->show_errors()    开启显示SQL报错(在执行语句前开启)

$wpdb->last_error    获取上一次SQL语句的执行错误信息

$wpdb->print_error()    打印当前执行的最后一条SQL错误信息(需要先开启报错)

$wpdb->hide_errors()    隐藏SQL报错

文章相关

have_posts()    判断是否有文章存在

get_post()/the_post()    获取或输出文章

get_posts()    根据传入的限定条件来显示多篇文章

get_permalink()    获取文章或页面链接

wp_list_pages()    获取页面列表

get_links()    获取友情链接

get_the_category()/the_category()    获取或输出文章分类

get_category_link()    获取分类链接

the_author()    输出文章作者

the_time(‘Y-m-d’)    输出文章发布时间(可自行定义格式)

edit_post_link(‘Edit’,’|’,”)    输出编辑文章链接(第一个值为输出的内容,第二个值为其前面输出的内容,第三个值为其后面输出的内容)

previous_post_link()/next_post_link()    当前文章上/下一篇文章标题链接(可自定义相关的文本,如previous_post_link(‘上一篇:%link’))

get_post_meta()    获取通过自定义栏目中所设定的值,如(get_post_meta($post->ID, ‘test’, true),true获取字符串,false获取数组,若名称前带下划线的则会在后台中隐藏,无法直接编辑),相关的函数还有add_post_meta(), update_post_meta(),和delete_post_meta()

get_the_ID()    获取文章的id号,等同于$post->ID

comments_open()    判断文章是否开启了评论功能

have_comments()    判断是否存在评论

wp_list_comments()    输出评论信息

comment_form()    输出评论表单

post_password_required()    判断是否密码保护的文章

is_user_logged_in()    判断用户是否登录

wp_login_url()    登录链接

get_search_form()    获取搜索框

get_search_query()  获取当前搜索词,也可使用$s来进行获取

wp_nav_menu()    调取导航,有多个菜单供选择时可传入array(‘menu’=>’test’)来获取指定菜单

is_home()/is_category()/is_search()/is_404()/is_page()/is_single()    判断是否首页、分类页、搜索页、404页、独立页面、文章页

$paged    当前页数,第一页值为0

获取所属的第一个分类名

$categories = get_the_category();
 
if ( ! empty( $categories ) ) {
    echo esc_html( $categories[0]->name );   
}

 

想要查看更全的参考手册,请参见:https://developer.wordpress.org/reference/functions/

喜欢 (1)
[]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址