前端邮政保存子条款

时间:2017-10-18 作者:730wavy

我正在尝试更新我的前端帖子表单,该表单允许我保存自定义分类法父术语,但现在我需要保存所选的子术语。

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>"; 
?>

2 个回复
最合适的回答,由SO网友:730wavy 整理而成

好吧,我用@TomMorton的建议解决了这个问题。

我必须更新我的代码以更新条款--

$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);

$child_term = get_term_by(\'slug\', $nhb_type_value, \'city-type\');
$term_array = array();
/*Find child ID the user selected*/
while ($child_term->parent != 0) {
    $child_term = get_term_by(\'id\', $child_term->ID, \'city-type\');
    //Get child term object and add to term_array
    array_push($term_array, $child_term->slug);
}
array_push($term_array, $nhb_type_value);
这是为了让他们--

$nhb_type_value = $_POST[\'nhb\'];

$nhb_type = wp_get_object_terms($Property_Id, \'city-type\', array(\'fields\' => \'ids\'));
$nhb_type = array_reverse($nhb_type);
                if (!empty($nhb_type)) {
                    $terms = get_term($nhb_type[0], \'city-type\');
                    $nhb_type_value = $terms->slug;
                }
我在下拉列表中唯一需要更改的是,我使用的是术语名称,但我将其更改为术语slug-

$selected = ($nhb_type_value == $term->slug) ? "selected" : "";
然后,只需使用--

wp_set_object_terms($pid, $nhb_type_value, \'city-type\');

SO网友:Tom

通过保存儿童条款,我相信您可以将其添加到$term_array 父级所在的组。从帖子(或自定义帖子类型)的角度来看,保存分类法与父级或子级无关。

WP Codex声明"For hierarchical terms (such as categories), you must always pass the id rather than the term name to avoid confusion where there may be another child with the same name." 这意味着您应该能够传递子ID,并且它将在子术语中进行分类。

资料来源:https://codex.wordpress.org/Function_Reference/wp_set_post_terms#Notes

关于显示子对象,您可以使用以下两种方法之一。首先,您可以根据您的代码继续遍历子级:

/*
Display child terms:

Source:
https://developer.wordpress.org/reference/functions/get_term_children/
*/
$parent_terms = wp_get_post_terms(get_the_ID(), \'city-type\');
if ( ! empty( $parent_terms ) && ! is_wp_error( $parent_terms ) ){
    foreach ( $parent_terms as $parent ) {
        echo \'\' . $parent->name . \'\';

        $children = get_term_children($parent->ID, \'city-type\'); //term_id, taxonomy
        if(!empty($children) && ! is_wp_error( $children )){
            foreach($children as $child){
                echo \'\' . $child->name . \'\';
            }
        }
    }
}
或者,您可以在从父级获取子级后循环遍历它们(如果您可能有父级,但需要它的子级:

/* OR YOU CAN GET CHILDREN DIRECTLY */
$children = get_terms(array(
    \'parent\' => 10 //ID_OF_PARENT_YOU_WANT
));

if(!empty($children) && ! is_wp_error( $children )){
    foreach($children as $child){
        echo \'\' . $child->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);

/*Find child ID the user selected*/
while ($child_term->parent != 0) {
    $child_term = get_term_by(\'id\', $child_term->ID, \'city-type\');
    //Get child term object and add to term_array
    array_push($term_array, $child_term->slug);
}

wp_set_object_terms($pid, $term_array, \'city-type\');

结束

相关推荐

按多个元值对Get_Terms进行排序

我正在尝试获取自定义分类法的所有术语(ediciones) 但由两个ACF字段排序:edicion_ano (年份字段)和edicion_numero (数字字段)。第一个应按年份排序,第二个应按数字字段排序。我从ACF(5.5.X)了解到,可以使用meta_query 我正在使用版本5.5.14。在新版本的ACF(5.5.x)中,现在使用术语元,WP允许对术语进行元查询,因此不再需要这些解决方法。(source)现在我正在使用以下代码:$args = array( \'taxonomy\