根据使用的表单和下拉列表分配类别

时间:2017-12-22 22:15:48

标签: wordpress frontend categories

我已经设置了所有内容,用于添加来自前端的帖子,选择类别,添加图片,文件。

用户可以从自动必须具有类别的表单发布,例如"小型服务"。

我有几个"主要"例如" car"," motor"。他们都有"小型服务"作为子类别。

当用户选择例如"车"从下拉列表中,Wordpress必须自动将帖子分配到类别" car"及其子类别"小型服务"。

$postTitleError = '';

if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && 
 wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {

if(trim($_POST['postTitle']) === '') {
    $postTitleError = 'Molimo Vas da unesete naslov.';
    $hasError = true;
} else {
    $postTitle = trim($_POST['postTitle']);
}
$cat_ID = get_cat_ID( $_POST['newcat'] );

//If not create new category
if($cat_ID == 0) {
   $cat_name = array('cat_name' => $_POST['newcat']);
   wp_insert_category($cat_name);
 }

  //Get ID of newly created category
 $new_cat_ID = get_cat_ID($_POST['newcat']);

$post_information = array(
    'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
    'post_content' => esc_attr(strip_tags($_POST['postContent'])),
    'post-type' => 'post',
    'post_status' => 'publish',
    'post_category' => array($new_cat_ID)

);

$post_id = wp_insert_post($post_information);

if($post_id)
{

    // Update Custom Meta
update_post_meta($post_id, 'vsip_custom_date', esc_attr(strip_tags($_POST['customMetaDate'])));
    update_post_meta($post_id, 'vsip_custom_brojilo', esc_attr(strip_tags($_POST['customMetaBrojilo'])));
    update_post_meta($post_id, 'vsip_custom_ulje', esc_attr(strip_tags($_POST['customMetaUlje'])));
        update_post_meta($post_id, 'vsip_custom_filter', esc_attr(strip_tags($_POST['customMetaFilter'])));
        update_post_meta($post_id, 'vsip_custom_permant', esc_attr(strip_tags($_POST['customMetaPermant'])));
        update_post_meta($post_id, 'vsip_custom_zamjena', esc_attr(strip_tags($_POST['customMetaZamjena'])));
update_post_meta($post_id, 'vsip_custom_file', esc_attr(strip_tags($_POST['customMetaFile'])));

    // Redirect
    wp_redirect( home_url() ); exit;
 }

 } 
  ?>
 <?php
 if( ! empty( $_FILES ) ) {
 foreach( $_FILES as $file ) {
   if( is_array( $file ) ) {
     $attachment_id = upload_user_file( $file );
   }
 }
 }
  ?>
    <?php get_header(); ?>
<!-- #primary BEGIN -->
 <div id="primary">
  <form action="" id="primaryPostForm" method="POST" enctype="multipart/form-data">
  <fieldset>
     <select id="cat" class="cat" name="newcat"  >
        <option value="0"><?php echo esc_attr(__('Izaberite vozilo')); ?></option>
        <?php
           $categories= get_categories(array('orderby ' => 'id' , 'order' => 'DESC' ,'hide_empty' => 0 , 'hierarchical' => true)); 
           foreach ($categories as $category) {
           $option = '<option value="'.$category->cat_name.', mali servis" broj="'.$category->category_count.'" val="'.$category->cat_name.'">';
           $option .= $category->cat_name; 
           $option .= '</option>';
           echo $option;
           }
           ?>
     </select>
  </fieldset>
  <fieldset>

0 个答案:

没有答案