我正在尝试更新我的前端帖子表单,该表单允许我保存自定义分类法父术语,但现在我需要保存所选的子术语。
NHB表示邻里关系,是城市类型分类法的子术语。下面是我的相关字段代码--
$city_type = wp_get_object_terms($Property_Id, \'city-type\', array(\'fields\' => \'ids\'));
$city_type = array_reverse($city_type);
if (!empty($city_type)) {
$city_term = get_term($city_type[0], \'city-type\');
$city_type_value = $city_term->slug;
}
$nhb = wp_get_object_terms($Property_Id, \'city-type\', array(\'fields\' => \'ids\'));
if (!empty($nhb)) {
$term = get_term($nhb[0], \'city-type\');
$nhb_type_value = $term->name;
以及-
wp_set_object_terms($pid, $nhb_type_value, \'city-type\');
update_post_meta($pid, \'imic_property_custom_city\', $property_custom_city);
$city_for_update = get_term_by(\'slug\', $city_type_value, \'city-type\');
$term_array = array();
while ($city_for_update->parent != 0) {
$city_for_update = get_term_by(\'id\', $city_for_update->parent, \'city-type\');
array_push($term_array, $city_for_update->slug);
}
array_push($term_array, $city_type_value);
wp_set_object_terms($pid, $term_array, \'city-type\');
然后我在前端的post表单上下拉列表,查看儿童术语--
<?php $taxonomyName = "city-type";
$parent_terms = get_terms( $taxonomyName, array( \'parent\' => 0, \'orderby\' => \'slug\', \'hide_empty\' => false ) );
echo "<select name=\'nhb\' class=\'form-control\' id=\'p-nhb\'>";
echo "<option class=\'button\' value=\'Any\'>All</option>";
foreach ( $parent_terms as $pterm ) {
//Get the Child terms
$terms = get_terms( $taxonomyName, array( \'parent\' => $pterm->term_id, \'orderby\' => \'slug\', \'hide_empty\' => false ) );
foreach ( $terms as $term ) {
$selected = ($nhb_type_value == $term->name) ? "selected" : "";
echo "<option data-val=\'" . $pterm->slug . "\' value=\'" . $term->slug . "\' \' . $selected . \'>" . $term->name . "</option>";
}
}
echo "</select>";
?>
如何将其另存为子项并输出?我当前输出的父项如下--
<?php $terms = wp_get_post_terms(get_the_ID(), \'city-type\');
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo \'\' . $term->name . \'\';
}
} ?>
UPDATE
在我的页面顶部,我有以下内容来保存条款$city\\u type是“city type”分类法的父术语$nhb表示与所选/保存的父术语相对应的子术语。
<?php
/*
Template Name: Front End Form
*/
get_header();
global $current_user, // Use global
get_currentuserinfo(); // Make sure global is set, if not set it.
$subdraft = $_POST[\'subdraft\'];
$edit_url = imic_get_template_url(\'template-edit-property-new.php\');
if ((user_can($current_user, "administrator"))||(user_can($current_user, "edit_others_posts")) ):
global $imic_options;
$msg = \'\';
$flag = 0;
$Property_Id = $property_title = $city_type_value = $nhb_type_value = \'\';
if (get_query_var(\'site\')) {
$Property_Id = get_query_var(\'site\');
$property_title = get_the_title($Property_Id);
$city_type = wp_get_object_terms($Property_Id, \'city-type\', array(\'fields\' => \'ids\'));
$city_type = array_reverse($city_type);
if (!empty($city_type)) {
$city_term = get_term($city_type[0], \'city-type\');
$city_type_value = $city_term->slug;
}
$nhb = wp_get_object_terms($Property_Id, \'city-type\', array(\'fields\' => \'ids\'));
if (!empty($nhb)) {
$term = get_term($nhb[0], \'city-type\');
$nhb_type_value = $term->name;
}
}
$Property_Status = get_post_meta(get_the_ID(), \'imic_property_status\', true);
// Check if the form was submitted
if (\'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty($_POST[\'action\'])) {
$property_title = $_POST[\'title\'];
$nhb_type_value = $_POST[\'nhb\'];
if (isset($_POST[\'textonomies_city\']) && !empty($_POST[\'textonomies_city\'])) {
$reverce_data = array_reverse($_POST[\'textonomies_city\']);
foreach ($reverce_data as $textonomies_city) {
if (!empty($textonomies_city)) {
$city_type_value = $textonomies_city;
break;
}
}
$property_custom_city = \'\';
}
if (($city_type_value == \'other\') || ($city_type_value == \'city\')) {
$city_type_value = \'\';
}
if ($msg == \'\') {
if (get_query_var(\'site\')) {
$post = array(
\'ID\' => get_query_var(\'site\'),
\'post_title\' => $property_title,
\'post_content\' => $property_content,
\'post_date\' => $property_listdate_value,
\'post_status\' => \'publish\', // Choose: publish, preview, future, etc.
\'post_type\' => \'property\' // Use a custom post type if you want to
);
$pid = wp_update_post($post);
// Pass the value of $post to WordPress the insert function
$flag = 1;
} else {
$post_status = \'draft\';
}
$post = array(
\'post_title\' => $property_title,
\'post_content\' => $property_content,
\'post_status\' => $post_status,
\'post_date\' => $property_listdate_value,
\'post_type\' => \'property\' // Use a custom post type if you want to
);
$pid = wp_insert_post($post);
$total_property = get_user_meta($current_user->ID, \'property_value\', true);
$new_value = ($total_property != 0) ? ($total_property - 1) : $total_property;
update_user_meta($current_user->ID, \'property_value\', $new_value);
$flag = 1;
}
wp_set_object_terms($pid, $nhb_type_value, \'city-type\');
if (\'POST\' == $_SERVER[\'REQUEST_METHOD\']) {
// Set Terms For Tax
wp_set_object_terms($pid, $nhb_type_value, \'city-type\');
$city_for_update = get_term_by(\'slug\', $city_type_value, \'city-type\');
$term_array = array();
while ($city_for_update->parent != 0) {
$city_for_update = get_term_by(\'id\', $city_for_update->parent, \'city-type\');
array_push($term_array, $city_for_update->slug);
}
array_push($term_array, $city_type_value);
wp_set_object_terms($pid, $term_array, \'city-type\');
if (get_query_var(\'site\')) {
$Property_Id = get_query_var(\'site\');
$property_title = get_the_title($Property_Id);
$nhb = wp_get_object_terms($Property_Id, \'city-type\', array(\'fields\' => \'ids\'));
if (!empty($nhb)) {
$terms = get_term($nhb[0], \'city-type\');
$nhb_type_value = $terms->name;
}
}
}
}
if(get_query_var(\'remove\')){
$delete_id = get_query_var(\'remove\');
$post_author = get_post_field(\'post_author\',$delete_id);
$user_name= $current_user->ID;
if($post_author==$user_name){
wp_trash_post($delete_id); }
}
if (get_query_var(\'site\')) {
$current_Id = get_query_var(\'site\');
} else {
$current_Id = get_the_ID();
}
?>
表格-
<form action="" method="post" enctype="multipart/form-data">
子术语下拉列表选择-
<?php $taxonomyName = "city-type";
$parent_terms = get_terms( $taxonomyName, array( \'parent\' => 0, \'orderby\' => \'slug\', \'hide_empty\' => false ) );
echo "<select name=\'nhb\' class=\'form-control\' id=\'p-nhb\'>";
echo "<option class=\'button\' value=\'Any\'>All</option>";
foreach ( $parent_terms as $pterm ) {
//Get the Child terms
$terms = get_terms( $taxonomyName, array( \'parent\' => $pterm->term_id, \'orderby\' => \'slug\', \'hide_empty\' => false ) );
foreach ( $terms as $term ) {
$selected = ($nhb_type_value == $term->name) ? "selected" : "";
echo "<option data-val=\'" . $pterm->slug . "\' value=\'" . $term->slug . "\' " . $selected . ">" . $term->name . "</option>";
}
}
echo "</select>";
?>