Skip to content

快速参考

常用函数速查

文章操作

php
<?php
// 获取文章
the_post();              // 循环中获取当前文章
the_ID();                // 输出文章 ID
get_the_ID();            // 返回文章 ID
the_title();             // 输出文章标题
get_the_title($post_id); // 返回文章标题
the_content();           // 输出文章内容
get_the_content();       // 返回文章内容
the_excerpt();           // 输出文章摘要
get_the_excerpt();       // 返回文章摘要

// 文章信息
the_permalink();         // 输出文章链接
get_the_permalink($post_id);
the_author();            // 输出作者名
get_the_author();
the_date();              // 输出发布日期
the_time();              // 输出发布时间
the_modified_date();     // 输出修改日期

// 文章图片
has_post_thumbnail();    // 检查是否有缩略图
the_post_thumbnail();    // 输出缩略图
the_post_thumbnail('medium');
the_post_thumbnail('large');
the_post_thumbnail(array(300, 200));

// 分类和标签
the_category();          // 输出分类
the_tags();              // 输出标签
get_the_category();
get_the_tags();

循环结构

php
<?php
// 标准循环
if (have_posts()) {
    while (have_posts()) {
        the_post();
        the_title();
        the_content();
    }
}

// 自定义查询
$query = new WP_Query(array('post_type' => 'portfolio'));
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        the_title();
    }
    wp_reset_postdata();
}
?>

查询参数

php
<?php
// WP_Query 常用参数
$args = array(
    'post_type'      => 'post',           // 文章类型
    'posts_per_page' => 10,              // 每页数量 (-1 全部)
    'paged'          => 1,               // 页码
    'orderby'        => 'date',          // 排序字段
    'order'          => 'DESC',          // 升序/降序
    'post_status'    => 'publish',       // 文章状态
    
    // 分类查询
    'cat'            => 5,               // 分类 ID
    'category_name'  => 'news',          // 分类别名
    
    // 标签查询
    'tag'            => 'featured',      // 标签别名
    'tag_id'         => 10,              // 标签 ID
    
    // 作者查询
    'author'         => 1,               // 作者 ID
    'author_name'    => 'john',
    
    // 搜索
    's'              => 'keyword',
    
    // 自定义字段
    'meta_query'     => array(
        array(
            'key'     => 'featured',
            'value'   => '1',
            'compare' => '=',
        ),
    ),
);

// 排序选项
'orderby' => 'date', 'title', 'modified', 'author', 'ID', 
             'menu_order', 'rand', 'comment_count', 'meta_value'

// 关系查询
'tax_query' => array(
    'relation' => 'AND',  // 或 OR
    array(
        'taxonomy' => 'category',
        'field'    => 'slug',
        'terms'    => array('news'),
    ),
),
?>

模板标签

php
<?php
// 头部和底部
get_header();
get_footer();
get_sidebar();

// 部分模板
get_template_part('content', 'page');
get_template_part('template-parts/loop', 'archive');

// 导航菜单
wp_nav_menu(array(
    'theme_location' => 'primary',
    'container'      => false,
    'menu_class'     => 'nav-menu',
    'fallback_cb'    => false,
));

// 小工具
dynamic_sidebar('sidebar-id');

// 条件标签
is_home();              // 首页
is_front_page();         // 前台页面
is_single();             // 单文章
is_page();               // 页面
is_archive();            // 存档页
is_category();           // 分类页
is_tag();                // 标签页
is_search();             // 搜索结果
is_404();                // 404 页面
is_singular();           // 任何单内容
?>

用户相关

php
<?php
// 获取当前用户
$current_user = wp_get_current_user();
$current_user->ID;
$current_user->user_email;
$current_user->display_name;
current_user_can('edit_posts');

// 用户信息
get_userdata($user_id);
get_users($args);
wp_get_current_user();

// 用户元数据
get_user_meta($user_id, 'key', $single);
update_user_meta($user_id, 'key', $value);
?>

分类和标签

php
<?php
// 获取分类
get_the_category();
wp_get_post_categories($post_id);

// 获取标签
get_the_tags();
wp_get_post_tags($post_id);

// 术语链接
get_term_link($term);
the_terms($post_id, 'category');

// 注册的分类法
$categories = get_categories(array(
    'type'         => 'post',
    'child_of'     => 0,
    'parent'       => 0,
    'orderby'      => 'name',
    'order'        => 'ASC',
    'hide_empty'   => 1,
));

$tags = get_tags(array(
    'hide_empty' => 1,
));
?>

媒体文件

php
<?php
// 获取附件
wp_get_attachment_url($attachment_id);
wp_get_attachment_image($attachment_id, 'medium');
wp_get_attachment_metadata($attachment_id);

// 上传文件
wp_upload_bits($name, $deprecated, $upload);
media_sideload_image($url, $post_id, $desc);

// 媒体库查询
$attachments = get_posts(array(
    'post_type'      => 'attachment',
    'post_parent'    => $post_id,
    'post_mime_type' => 'image',
    'posts_per_page' => -1,
));
?>

URL 和链接

php
<?php
// 站点 URL
site_url();
home_url();
admin_url();
includes_url();
content_url();
plugin_dir_url(__FILE__);
get_template_directory_uri();

// 永久链接
get_permalink($post_id);
the_permalink();
get_post_type_archive_link('portfolio');

// 导航链接
get_previous_post();
get_next_post();
previous_posts_link();
next_posts_link();
?>

安全函数

php
<?php
// 转义
esc_html($text);
esc_attr($class);
esc_url($url);
esc_js($string);
wp_kses_post($html);
wp_kses($html, $allowed_tags);
esc_textarea($text);

// 清理
sanitize_text_field($str);
sanitize_email($email);
sanitize_file_name($filename);
sanitize_key($key);
sanitize_title($title);
sanitize_html_class($class);
sanitize_textarea_field($str);

// 验证
wp_verify_nonce($nonce, $action);
current_user_can($capability);
is_email($email);
wp_check_filetype($filename);
?>

钩子速查

php
<?php
// 动作钩子
add_action('init', 'function');
add_action('wp_enqueue_scripts', 'enqueue_assets');
add_action('wp_head', 'output_head');
add_action('wp_footer', 'output_footer');
add_action('the_content', 'filter_content');
add_action('save_post', 'save_callback');

// 过滤器钩子
add_filter('the_content', 'modify_content');
add_filter('excerpt_length', 'custom_length');
add_filter('excerpt_more', 'custom_more');
add_filter('wp_title', 'custom_title');
add_filter('body_class', 'add_body_class');

// 优先级和参数
add_action('hook', 'func', 10, 3);  // 优先级 10, 3 个参数
remove_action('hook', 'func', 10);
?>

数据库

php
<?php
global $wpdb;

// 常用操作
$results = $wpdb->get_results("SELECT * FROM $wpdb->posts");
$row = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = %d", $id);
$value = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts");

$wpdb->insert($table, $data, $format);
$wpdb->update($table, $data, $where, $format, $where_format);
$wpdb->delete($table, $where, $where_format);

// 安全查询
$wpdb->prepare(
    "SELECT * FROM $wpdb->posts WHERE ID = %d AND post_type = %s",
    $id,
    $post_type
);

// Meta 操作
get_post_meta($post_id, $key, $single);
update_post_meta($post_id, $key, $value);
add_post_meta($post_id, $key, $value);
delete_post_meta($post_id, $key);

// Transients
set_transient('key', $data, $expiration);
get_transient('key');
delete_transient('key');
?>

选项 API

php
<?php
// 获取选项
get_option('option_name', $default);
get_site_option('site_option');

// 更新选项
update_option('option_name', $value);
update_site_option('site_option', $value);

// 删除选项
delete_option('option_name');

// 自动加载选项
add_option('name', 'value', '', 'yes');  // autoload = yes
?>

WordPress 常量

php
<?php
ABSPATH              // WordPress 根目录路径
WPINC                // wp-includes 目录
WP_CONTENT_DIR       // wp-content 目录
WP_PLUGIN_DIR        // 插件目录
TEMPLATEPATH         // 当前主题目录路径
STYLESHEETPATH      // 当前主题样式表目录路径
WP_MEMORY_LIMIT      // WordPress 内存限制
WP_MAX_MEMORY_LIMIT  // WordPress 最大内存限制
WP_DEBUG             // 调试模式
DONOTCACHEPAGE       // 禁止页面缓存
SHORTINIT            // 最小化加载
WP_USE_THEMES        // 是否使用主题
?>

数据库表速查

表名说明
wp_posts文章、页面、附件
wp_postmeta文章元数据
wp_comments评论
wp_commentmeta评论元数据
wp_terms分类/标签术语
wp_term_taxonomy分类法定义
wp_term_relationships文章-术语关联
wp_termmeta术语元数据
wp_users用户
wp_usermeta用户元数据
wp_options选项/设置

常用快捷方式

操作方法
特色图片has_post_thumbnail(), the_post_thumbnail()
翻页导航the_posts_pagination(), posts_nav_link()
评论comments_open(), comments_number()
文章作者get_the_author(), get_author_posts_url()
搜索表单get_search_form()
日历get_calendar()
RSS 链接bloginfo('rss2_url')
语言文件load_textdomain()

基于 WordPress官方文档 构建