当我从下拉列表中选择类别时,一切都正常。但在后端,它只选中了子项,而不是父项

时间:2018-04-25 作者:phpdeveloper aegisbit

这里是我的代码

<?php if(isset($_POST[\'new_post\']) == \'1\') {
    $post_title = $_POST[\'post_title\'];
    $post_category = $_POST[\'cat\'];
    $post_content = $_POST[\'post_content\'];
    $parent = get_the_tags($post_category);;




    $new_post = array(
          \'ID\' => \'\',
          \'post_author\' => $user->ID, 
          \'post_category\' => array($parent,$post_category),
          \'post_type\' => \'currencies\',
          \'post_content\' => $post_content, 
          \'post_title\' => $post_title,
          \'post_status\' => \'publish\'
        );

    $post_id = wp_insert_post($new_post); print_r($post_id);
    // This will redirect you to the newly created post
    $post = get_post($post_id);    echo "Done";
    }       ?>   

<h1fhsdfh</h1> <form method="post" action=""> 
    <label>Post-Title:-</label><br><label><input type="text"  class="form-control"  name="post_title" size="45"
id="input-title"/></label><br>   
<label>Catogries:-</label><br><label > <?php
wp_dropdown_categories(\'orderby=name&hide_empty=0&exclude=1&hierarchical=1\');
?> </label><br>

    <label>Content:-</label><br><label><textarea  class="form-control"  rows="5" name="post_content" cols="66"
id="text-desc"></textarea> 
    <input type="hidden" name="new_post" value="1"/> </label>
    <input class="subput round form-control"   type="submit" name="submit" value="Post"/> </form> <?php get_footer(); ?>
请查看此图像http://prntscr.com/j9toiz

1 个回复
SO网友:Quang Hoang

您可以在wp admin中添加自定义列以显示父类别。这是我的代码,你可以试试(我用post_typepost (默认值))。

 // Add the custom columns to the post post type:
add_filter( \'manage_post_posts_columns\', \'set_custom_edit_post_columns\' );
function set_custom_edit_post_columns($columns) {
    $columns[\'parent_cat\'] = __( \'Parent Category\', \'storefront\' );
    return $columns;
}

// Add the data to the custom columns for the post post type:
add_action( \'manage_post_posts_custom_column\' , \'custom_post_column\', 10, 2 );
function custom_post_column( $column, $post_id ) {
    if($column == \'parent_cat\'){
        $terms = get_terms(\'category\');
        foreach ($terms as $term) {
            if($term->parent == 0){
                echo $term->name;
            }
        }
    }
}
以下是要查看的屏幕截图:http://nimb.ws/RpoV8W

结束