【发布时间】:2011-12-19 13:04:27
【问题描述】:
我希望我的用户能够从前端发帖,并将帖子放在他们创建的类别中。除了创建新类别的可能性之外,我的表单中的所有内容都已完成。
有人知道怎么做吗?
谢谢!!
【问题讨论】:
标签: wordpress categories frontend
我希望我的用户能够从前端发帖,并将帖子放在他们创建的类别中。除了创建新类别的可能性之外,我的表单中的所有内容都已完成。
有人知道怎么做吗?
谢谢!!
【问题讨论】:
标签: wordpress categories frontend
使用
<?php wp_create_category( $cat_name, $parent ); ?>
传递变量
wp_create_category($category);
或,
挂钩 create_category 操作
【讨论】:
假设你使用 POST
//Checking if category already there
$cat_ID = get_cat_ID( $_POST['newcat'] );
//If not create new category
if($cat_ID == 0) {
$cat_name = array('cat_name' => $_POST['newcat']);
wp_insert_category($cat_name);
}
//Get ID of newly created category
$new_cat_ID = get_cat_ID($_POST['newcat']);
// Create post object
$new_post = array(
'post_title' => $headline,
'post_content' => $body,
'post_excerpt' => $excerpt,
'post_date' => $date,
'post_date_gmt' => $date,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array($new_cat_ID)
);
// Insert the post into the database
wp_insert_post( $new_post );'
你也可以输入这个:$newcat = $_POST['newcat'] and then just replace with $newcat in the code
这可能看起来更好:-)
请注意,就您而言,wp_insert_category() 和 wp-create_categoy() 具有相同的功能(恕我直言)
【讨论】:
add_action( 'admin_init', function(){
wp_create_category( 'Custom Category 1' );
wp_create_category( 'Custom Category 2' );
});
【讨论】:
如果您想从前端添加类别,那么有一种简单的方法:(100% 工作)
wp_insert_term('Category Name', 'texonomy_type');
示例:wp_insert_term('National', 'category');
【讨论】: