手机购物网站 设计,销售网站的销量统计怎么做,企业建网站好,10个神奇的.htaccess技巧(for wordpress)在使用Wordpress做前端投稿功能的时候#xff0c;可能需要用户填写文章标签#xff0c;在插入文章的时候很多人不知道怎么把这些标签插入进去#xff0c;下面这篇文章来为大家带来WordPress使用前端投稿功能时插入文章标签方法。 在Wordpress里 wp_insert_post 此函数的作… 在使用Wordpress做前端投稿功能的时候可能需要用户填写文章标签在插入文章的时候很多人不知道怎么把这些标签插入进去下面这篇文章来为大家带来WordPress使用前端投稿功能时插入文章标签方法。 在Wordpress里 wp_insert_post 此函数的作用是插入文章(或页面、自定义文章类型)到数据库插入之前会净化一些变量做一些检查补全一些缺失的必需数据(如日期/时间)。此函数需要一个数组作为参数插入成功后返回插入文章的 ID(插入失败返回 0)。使用插入文章中的 tags_input 参数配置就可以了可以参考如下代码
$tougao array(ID $post_id,post_title $title,post_content $content,post_category array($zhuanlan),tags_input $tags,
//格式如array(mobantu,mbt)所以变量$tags应该是个数组形式也就是要求从投稿表单中获取到的填入的标签变成数组形式赋值给变量$tagspost_author $current_user_id,post_status pending,
);
$status wp_insert_post( $tougao ); 上面是 WordPress 前端投稿时怎么插入文章标签 代码片段用到的是 wp_insert_post 函数下面我们具体讲下 wp_insert_post 函数。 为post[ID]设置一个值将不会创建 ID 为该值的文章而是更新 ID 为该值的文章也就是说要想插入一篇新文章$post[ID]必须为空或者压根不设置。 文章数组的内容取决于你对文章默认值的理解程度下面是所有文章数组元素的简短描述。
$post array(ID [ post id ] // 如果需要更新文章设置id为需要更新文章的id否则不要设置此值post_content [ string ] // 文章内容也就是可视化编辑器里面的输入的内容post_name [ string ] // 文章的别名就是URL里面的名称post_title [ string ] // 文章标题post_status [ draft | publish | pending| future | private | custom registered status ] // 文章状态默认 draft.post_type [ post | page | link | nav_menu_item | custom post type ] // 文章类型默认为post.post_author [ user ID ] // 文章作者的ID默认为当前登录的用户IDping_status [ closed | open ] // 是否允许 Pingbacks 或 trackbacks allowed默认为default_ping_status 设置的值。post_parent [ post ID ] // 文章的父级文章ID默认为 0顶级文章。menu_order [ order ] // 如果新文章为一个页面可以设置一个页面序号默认为0。to_ping // 空格或回车分隔的需要ping的url列表默认为空字符串。pinged // 空格或回车分隔的已经ping过的url列表默认为空字符串。post_password [ string ] // 文章密码默认为空字符串。guid // 不要管这个WordPress会自动处理。post_content_filtered // 不要管这个WordPress会自动处理。post_excerpt [ string ] // 文章摘要。post_date [ Y-m-d H:i:s ] // 文章发布时间。post_date_gmt [ Y-m-d H:i:s ] // GMT格式的文章发布时间。comment_status [ closed | open ] // 是否允许评论默认为 default_comment_status的值或closed。post_category [ array(category id, ...) ] // 文章分类目录默认为空tags_input [ tag, tag, ... | array ] // 文章标签默认为空tax_input [ array( taxonomy array | string, taxonomy_other array | string ) ] // 文章的自定义分类法项目默认为空。page_template [ string ] // 页面模板文件的名称如template.php默认为空。
);
post_name, post_title, post_content, 和 post_excerpt 为必需的元素。‘post_status’如果设置了 post_status 为 ‘future’你还必须指定 post_date 值这样 WordPress 才能知道什么时候发布你的文章更多信息参见 文章状态转换。‘post_category’等效于调用 wp_set_post_categories()。‘tags_input’等效于调用 wp_set_post_tags()。‘tax_input等效于为数组中的每个自定义分类法调用 wp_set_post_terms()如果当前用户没有设置自定义分类法的权限就必须使用 wp_set_object_terms() 代替了。‘page_template’如果 post_type 为 ‘page’将尝试设置页面如果设置失败此函数将返回一个 WP_Error 对象或 0然后在最终操作之前停止。如果 post_type 不是 ‘page’此参数将被忽略你可以通过调用 update_post_meta() 设置 ‘_wp_page_template’ 的值为不是页面的文章类型设置页面模板。
如果文章成功插入了数据库将返回插入的新文章 ID如果失败将返回 0 或一个 WP_Error 对象如果$wp_error 设置为 true
使用示例 在调用 wp_insert_post() 之前我们需要创建一个包含必要文章元素的数组wp_insert_post() 将会使用默认值自动填充一些文章元素但是用户必须提供一个文章标题和内容否则将会出现数据库错误导致插入文章失败。 下面的例子中我么设置了 post title, content, status, author, 和 post categories除了这些我们可以根据上面的列表添加更多的文章元素键值对以匹配 wp_posts 数据表中的数据列。
// 创建文章对象
$my_post array(post_title 我的测试文章,post_content 这是一个测试文章。,post_status publish,post_author 1,post_category array(8,39)
);// 插入文章到数据库
文章插入成功后将返回新文章 id。
$post_id wp_insert_post( $post, $wp_error );
//现在我们可以使用 $post_id 来 add_post_meta 或 update_post_meta
上面提到的文章元素默认值为下面数组
$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 ,import_id 0 分类目录应该以分类 ID 数组的形式传入即使只需要设置一个分类目录该参数的值也必须为数组。
更多信息参见wp_set_post_terms()
安全问题 在存入数据库之前wp_insert_post() 先把数据传递给 sanitize_post() 处理了也就是说该函数已经处理了所有的数据验证和净化我们不需要再为这些问题操心了。 因为一些原因你可能需要移除文章标题或其他字段中的 HTML, JavaScript, 和 PHP 代码奇怪的是 WordPress 竟然没有自动处理这些事情不过我们可以使用 wp_strip_all_tags() 函数(WordPress 2.9 以后可用)轻松的搞定这在提交前端表单的时候特别有用。
// 创建文章对象
$my_post array(post_title wp_strip_all_tags( $_POST[post_title] ),post_content $_POST[post_content],post_status publish,post_author 1,post_category array( 8,39 )
);// 插入文章到数据库
wp_insert_post( $my_post );
到此结束。