一、核心文章函数
1. 文章获取函数
wp_insert_post() – 创建新文章
$post_data = array(
'post_title' => '文章标题',
'post_content' => '文章内容',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'post'
);
$post_id = wp_insert_post($post_data);
wp_update_post() – 更新现有文章
$update_data = array(
'ID' => $post_id,
'post_title' => '更新后的标题',
'post_content' => '更新后的内容'
);
wp_update_post($update_data);
get_post() – 获取文章对象
$post = get_post($post_id);
echo $post->post_title; // 输出标题
echo $post->post_content; // 输出内容
2. 文章查询函数
WP_Query类 – 强大的查询类
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
the_title(); // 输出标题
the_content(); // 输出内容
}
wp_reset_postdata();
}
get_posts() – 简化查询
$posts = get_posts(array(
'numberposts' => 3,
'category' => 1
));
foreach ($posts as $post) {
setup_postdata($post);
the_title();
}
wp_reset_postdata();
二、文章内容相关函数
1. 内容输出函数
the_content(); // 输出完整内容(带格式)
the_excerpt(); // 输出摘要
get_the_content(); // 获取内容(返回字符串)
get_the_excerpt(); // 获取摘要(返回字符串)
2. 标题处理函数
the_title(); // 输出标题
get_the_title(); // 获取标题
the_title_attribute(); // 输出标题属性(用于HTML属性)
三、文章元数据函数
get_post_meta() – 获取自定义字段
$value = get_post_meta($post_id, 'field_name', true);
update_post_meta() – 更新自定义字段
update_post_meta($post_id, 'field_name', '字段值');
add_post_meta() – 添加自定义字段
add_post_meta($post_id, 'new_field', '字段值', true);
delete_post_meta() – 删除自定义字段
delete_post_meta($post_id, 'field_name');
四、文章状态和类型函数
// 检查文章状态
if (is_single()) { } // 是否文章页
if (is_page()) { } // 是否页面
if (is_singular('book')) { } // 是否自定义文章类型
if (is_sticky($post_id)) { } // 是否置顶文章
五、循环相关函数
if (have_posts()) {
while (have_posts()) {
the_post();
// 循环内可用函数
the_ID(); // 文章ID
the_permalink(); // 文章链接
the_author(); // 作者
the_date(); // 日期
the_category(); // 分类
the_tags(); // 标签
comments_number(); // 评论数
edit_post_link(); // 编辑链接
}
}
六、实用示例
1. 创建带分类的文章
$post_id = wp_insert_post(array(
'post_title' => '新文章',
'post_content' => '内容',
'post_status' => 'publish',
'post_category'=> array(1, 3) // 分类ID
));
// 添加标签
wp_set_post_tags($post_id, 'WordPress, 教程, 开发');
2. 自定义查询分页
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $paged
);
$custom_query = new WP_Query($args);
// 分页导航
the_posts_pagination(array(
'mid_size' => 2,
'prev_text' => '上一页',
'next_text' => '下一页',
));
3. 获取相关文章
$categories = get_the_category($post_id);
$cat_ids = array();
foreach ($categories as $category) {
$cat_ids[] = $category->term_id;
}
$related_args = array(
'category__in' => $cat_ids,
'post__not_in' => array($post_id),
'posts_per_page' => 4,
'orderby' => 'rand'
);
$related_posts = new WP_Query($related_args);
七、性能优化建议
- 使用缓存:合理使用transient API缓存查询结果
- 避免重复查询:在循环外获取数据
- 使用WP_Query参数:只查询需要的字段
$args = array(
'fields' => 'ids' // 只获取ID,不查询完整对象
);
- 清理查询:及时使用wp_reset_postdata()和wp_reset_query()
八、最佳实践
- 始终检查函数返回值
- 使用适当的文章状态(publish, draft, private等)
- 处理HTML转义和安全问题
- 考虑多语言支持
- 遵循WordPress编码标准
总结
WordPress提供了丰富的文章处理函数,涵盖了从创建、查询、更新到显示的全过程。合理使用这些函数可以高效开发主题和插件,同时确保代码的性能和安全性。建议在实际开发中结合具体需求选择合适的函数组合,并注意遵循WordPress的开发规范和最佳实践。


湘公网安备43020002000238