我在尝试使用前端的表单创建具有分类法的自定义类型帖子时遇到问题。
我曾尝试添加几行代码并使用“tax\\u input”,但总是出现错误。
自定义帖子类型为:pet
分类法是pet-category
和pet-city
. 此代码工作正常,但只发布标题和内容。
如果有人能告诉我,为了发布这两个分类法,我需要添加什么,我将不胜感激。但我试了又试,但没有成功,
<?php /* Template Name: Insert Posts */
$postTitleError = \'\';
if(isset($_POST[\'submitted\']) && isset($_POST[\'post_nonce_field\']) && wp_verify_nonce($_POST[\'post_nonce_field\'], \'post_nonce\')) {
$postTitle = trim($_POST[\'postTitle\']);
$new_post = array(
\'post_title\' => wp_strip_all_tags( $_POST[\'postTitle\'] ),
\'post_content\' => $_POST[\'postContent\'],
\'post_type\' => \'pet\',
\'post_status\' => \'publish\'
);
$post_id = wp_insert_post($new_post,$wperror);
if($post_id)
{
// Update Custom Meta
update_post_meta($post_id, \'vsip_custom_one\', esc_attr(strip_tags($_POST[\'customMetaOne\'])));
update_post_meta($post_id, \'vsip_custom_two\', esc_attr(strip_tags($_POST[\'customMetaTwo\'])));
// Redirect
wp_redirect( home_url() ); exit;
}
} ?>
<?php get_header(); ?>
<!-- #primary BEGIN -->
<div id="primary">
<form action="" id="primaryPostForm" method="POST">
<!-- Post Title -->
<fieldset>
<label for="postTitle"><?php _e(\'Post\\\'s Title:\', \'framework\') ?></label>
<input type="text" name="postTitle" id="postTitle" value="<?php if(isset($_POST[\'postTitle\'])) echo $_POST[\'postTitle\'];?>" class="required" />
</fieldset>
<?php if($postTitleError != \'\') { ?>
<span class="error"><?php echo $postTitleError; ?></span>
<div class="clearfix"></div>
<?php } ?>
<!-- Post Content -->
<fieldset>
<label for="postContent"><?php _e(\'Post\\\'s Content:\', \'framework\') ?></label>
<textarea name="postContent" id="postContent" rows="8" cols="30"><?php if(isset($_POST[\'description\'])) { if(function_exists(\'stripslashes\')) { echo stripslashes($_POST[\'description\']); } else { echo $_POST[\'description\']; } } ?></textarea>
</fieldset>
<!-- Pet Category -->
<li>
<label for="pet-category">Categoria: *</label>
<select name="pet_category" id="pet_category" tabindex="9" class="required">
<option value=""></option>
<?php
$categories = get_terms(\'pet-category\', array(\'hide_empty\' => 0));
foreach ($categories as $category) {
echo "<option id=\'pet_category\' value=\'$category->slug\'>$category->name</option>";
}
?>
</select>
</li>
<!-- Pet City -->
<li>
<label for="pet-city">City: *</label>
<select name="pet_city" id="pet_city" tabindex="9" class="required">
<option value=""></option>
<?php
$cities = get_terms(\'pet-city\', array(\'hide_empty\' => 0));
foreach ($cities as $city) {
echo "<option id=\'pet_city\' value=\'$city->slug\'>$city->name</option>";
}
?>
</select>
</li>
<!-- Pet Image -->
<fieldset name="site-image" class="site-image">
<input type="file" name="image" class="file_input_hidden site-image file_upload" onchange="javascript: document.getElementById(\'fileName\').value = this.value;" />
<br />Al menos de 200 de ancho x 200 de alto
</fieldset>
<!-- Post Validation of Content and Submit Button -->
<fieldset>
<?php wp_nonce_field(\'post_nonce\', \'post_nonce_field\'); ?>
<input type="hidden" name="submitted" id="submitted" value="true" />
<button type="submit"><?php _e(\'Add Post\', \'framework\') ?></button>
</fieldset>
</form>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
最合适的回答,由SO网友:Nicolai Grossherr 整理而成
您的代码没有将术语分配给帖子。你可以用wp_set_post_terms() 其中包括:
设置帖子的条款。
简化使用示例:
$pet_cat_ term = $_POST[\'pet_category\'];
wp_set_post_terms( $post_id, $pet_cat_ term, \'pet-category\' );
附加说明:使用
id 作为术语的值,请选择以防止出现问题,即因为对于层次术语,您无论如何都必须这样做。
SO网友:Asha
Add term for custom taxonomy from front end through ajax
HTML
<div class="articles_catform">
<form id="articles_catform" name="articles_catform" class="wordpress-ajax-form2" method="post" action="<?php echo admin_url(\'admin-ajax.php\'); ?>" enctype="multipart/form-data" >
<input type="text" name="name" placeholder="Category Title">
<br>
<textarea name="description" placeholder="Category Description" style="height: 200px !important;"></textarea>
<br>
<input type="hidden" name="action" value="custom_cataction">
<br>
<button>Send</button>
</form>
</div>
JS公司
$(".wordpress-ajax-form2").submit(function(evt){
if( document.articles_catform.name.value == "") {
alert( "Please provide Title! " );
document.articles_catform.name.focus() ;
return false;
}
if(document.articles_catform.description.value == "" ) {
alert( "Please provide your Description! ");
document.articles_catform.description.focus() ;
return false;
}
evt.preventDefault();
var $form = $(this);
$.post($form.attr(\'action\'), $form.serialize(), function(data) {
//return false;
if(data){
alert(\'Category Created Successfully...\');
}
else {
alert(\'Already Exists!!!\');
}
});
});
Add In function file
add_action(\'wp_ajax_custom_cataction\', \'custom_cataction\');
add_action(\'wp_ajax_nopriv_custom_cataction\', \'custom_cataction\');
function custom_cataction() {
$post_name = (trim($_POST[\'name\']));
$post_description = (trim($_POST[\'description\']));
$cid = wp_insert_term($post_name, \'articles_category\', array(
\'description\' => $post_description,
));
if ( is_wp_error($cid) ) {
//echo $cid->get_error_message();
echo\'\';
}
else {
echo\'1\';
}
wp_die();
}