我正在尝试创建一个前端张贴表单,允许用户在学校关闭时创建张贴。我有4个自定义分类法:关闭学校、学习开放、安全到达、小时延迟。每个学校都有30所左右的学校。一旦帖子提交,我想显示关闭、开放、延迟等的学校。
如果我在后端构建帖子,它就会工作,并显示出我想要的一切。如果我从前端发帖,没有显示任何类别,因此我知道我存储分类法的方式有问题,但我尝试了许多论坛和技术,但没有成功。非常感谢您的帮助,谢谢。
if(isset($_POST[\'submit\'])){
$title = $_POST[\'title\'];
$description = $_POST[\'description\'];
$category = $_POST[\'cat\'];
$tags = $_POST[\'post_tags\'];
$terms = $_POST[\'terms\'];
$error_msg = array();
if($title == \'\'){
$error_msg[] = \'Please select an area and/or region.\';
}
if($description == \'\'){
$error_msg[] = \'Please write a description for the closure and/or delay.\';
}
if($category == \'-1\'){
$error_msg[] = \'Please identify the reason for the closure and/or delay.\';
}
else if( !$error_msg && \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == "new_post"){
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'post_category\' => array($_POST[\'cat\']), // Usable for custom taxonomies too
\'tags_input\' => array($tags),
\'tax_input\' => array(terms),
\'post_status\' => \'publish\', // Choose: publish, preview, future, draft, etc.
\'post_type\' => \'post\', //\'post\',page\' or use a custom post type if you want to
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
//SET OUR TAGS UP PROPERLY
wp_set_post_tags($pid, $_POST[\'post_tags\']);
//SET OUR POST TERMS, CUSTOM TAXONOMIES
wp_set_post_terms($pid, $_POST[\'terms\'], \'closed_schools\', \'safe_open\', \'safe_arrival\', \'hour_delay\', true);
//REDIRECT TO THE NEW POST ON SAVEs
$link = get_permalink( $pid );
wp_redirect( $link );
//POST THE POST
do_action(\'wp_insert_post\', \'wp_insert_post\');
}
}
我的表格是自己的
<form id="new_post" name="new_post" method="post" action="" class="wpcf7-form front-end-form" enctype="multipart/form-data">
<!-- post name -->
<fieldset name="name">
<label for="title" class="selection-title">Area and/or Region:</label>
<!--<input type="text" id="title" value="" tabindex="5" name="title" />-->
<select name="title" id="title">
<option value="">-Please select an area and/or region-</option>
</select>
</fieldset>
<!-- post Category -->
<fieldset class="category">
<label for="cat" class="selection-title">Reason For closure and/or delay:</label>
<?php wp_dropdown_categories(array(\'hide_empty\' => 0, \'name\' => \'cat\', \'orderby\' => \'name\', \'selected\' => $category->parent, \'hierarchical\' => true, \'show_option_none\' => __(\'Select an area and/or region\'))); ?>
</fieldset>
<!-- post Content -->
<fieldset>
<label for="closed_schools" class="selection-title">Closed Schools:</label>
<?php
$closed_schools = get_terms(\'closed_schools\', \'orderby=id&hide_empty=0\');
$counter = 0;
foreach ($closed_schools as $close) {
$counter++;
$option = \'<label for="\'.$close->slug.\'">\'.$close->name.\'</label>\';
$option .= \'<input type="checkbox" name="terms[]" id="\'.$close->slug.\'" value="\'.$close->slug.\'">\';
echo $option;
}
?>
</fieldset>
<fieldset>
<label for="study_open" class="selection-title">Open for Study Only:</label>
<?php
$study_only = get_terms(\'study_open\', \'orderby=id&hide_empty=0\');
$counter = 0;
foreach ($study_only as $study) {
$counter++;
$option = \'<label for="\'.$study->slug.\'">\'.$study->name.\'</label>\';
$option .= \'<input type="checkbox" name="terms[]" id="\'.$study->slug.\'" value="\'.$study->slug.\'">\';
echo $option;
}
?>
</fieldset>
<fieldset>
<label for="open_for_safe_arrival" class="selection-title">Open for Safe Arrivals Only:</label>
<?php
$safe_arrival = get_terms(\'safe_arrival\', \'orderby=id&hide_empty=0\');
$counter = 0;
foreach ($safe_arrival as $safe) {
$counter++;
$option = \'<label for="\'.$safe->slug.\'">\'.$safe->name.\'</label>\';
$option .= \'<input type="checkbox" name="terms[]" id="\'.$safe->slug.\'" value="\'.$safe->slug.\'">\';
echo $option;
}
?>
</fieldset>
<fieldset>
<label for="2_hour_delay" class="selection-title">2 Hour Delay:</label>
<?php
$hour_delay = get_terms(\'hour_delay\', \'orderby=id&hide_empty=0\');
$counter = 0;
foreach ($hour_delay as $delay) {
$counter++;
$option = \'<label for="\'.$delay->slug.\'">\'.$delay->name.\'</label>\';
$option .= \'<input type="checkbox" name="terms[]" id="\'.$delay->slug.\'" value="\'.$delay->slug.\'">\';
echo $option;
}
?>
</fieldset>
<fieldset class="content">
<label for="description" class="selection-title">Description and Notes:</label>
<textarea id="description" tabindex="15" name="description" cols="80" rows="10"></textarea>
</fieldset>
<fieldset class="submit">
<button type="submit" value="Submit Cancellation/Closure" name="submit" tabindex="40" id="submit"><span>Submit Cancellation/Closure</span></button>
<!--<input type="submit" value="Submit Cancellation/Closure" tabindex="40" id="submit" name="submit" />-->
</fieldset>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( \'new-post\' ); ?>
</form>
SO网友:bilcker
经过多次尝试和错误后,我发现了这一点。对$new\\u post数组进行了一些细微的更改。
else if( !$error_msg && \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == "new_post"){
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'post_category\' => array($_POST[\'cat\']), // Usable for custom taxonomies too
\'tags_input\' => array($tags),
\'tax_input\' => array(\'closed_schools\' => $_POST[\'terms\'], \'study_open\' => $_POST[\'terms\'], \'safe_arrival\' => $_POST[\'safe_arrival\'], \'hour_delay\' => $_POST[\'hour_delay\']), //Place all of our custom taxonomies into the posted variable terms as an array
\'post_status\' => \'publish\', // Choose: publish, preview, future, draft, etc.
\'post_type\' => \'post\', //\'post\',page\' or use a custom post type if you want to
);
还要在每个自定义分类法上使用wp\\u set\\u object\\u terms而不是wp\\u set\\u post\\u terms
//SET OUR POST TERMS, CUSTOM TAXONOMIES
wp_set_object_terms($pid, $_POST[\'terms\'], \'closed_schools\');
wp_set_object_terms($pid, $_POST[\'terms\'], \'study_open\');
wp_set_object_terms($pid, $_POST[\'terms\'], \'safe_arrival\');
wp_set_object_terms($pid, $_POST[\'terms\'], \'hour_delay\');