前端帖子提交自定义分类和类别

时间:2016-04-29 17:14:16

标签: php wordpress categories custom-taxonomy

在我的网站上,我根据这篇文章建立了一个发布前端的帖子:

http://voodoopress.com/review-of-posting-from-front-end-form%29/

我的所有帖子都有类别和自定义分类。

问题是我无法找到一种方法来保存defautt类别&自定义分类(地区)。

将此行添加到自定义分类法"区域"的代码中时,未保存分类和类别:

wp_set_post_terms($pid,array($_POST['reg']),'regions',true);

以我的形式:

 <!-- post Region -->
  <fieldset class="regions">
    <label for="reg">Régions :</label>
    <?php wp_dropdown_categories( 'tab_index=10&taxonomy=regions&hide_empty=0&show_option_all=Choisissez une Région' ); ?>
  </fieldset>

这是我在function.php

中的自定义分类
// CUSTOM REGION TAXONOMY
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 );

//create a custom taxonomy name it topics for your posts

function create_topics_hierarchical_taxonomy() {

// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI

  $labels = array(
    'name' => _x( 'Region', 'taxonomy general name' ),
    'singular_name' => _x( 'Region', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Regions' ),
    'all_items' => __( 'Toutes' ),
    'parent_item' => __( 'Parent Region' ),
    'parent_item_colon' => __( 'Parent Region:' ),
    'edit_item' => __( 'Edit Region' ), 
    'update_item' => __( 'Update Region' ),
    'add_new_item' => __( 'Add New Region' ),
    'new_item_name' => __( 'New Topic Region' ),
    'menu_name' => __( 'Régions' ),
  );    

// Now register the taxonomy

  register_taxonomy('regions',array('post'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'Region' ),
  ));

}

这是我的前端php:

<?php
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

  // Do some minor form validation to make sure there is content
  if (isset ($_POST['title'])) {
    $title =  $_POST['title'];
  } else {
    echo 'veuillez entrer un titre';
  }
  if (isset ($_POST['description'])) {
    $description = $_POST['description'];
  } else {
    echo 'veuillez entrer du texte';
  }

  $genre = $_POST['genre'];
  $tranche_age = $_POST['tranche_age'];

  // ADD THE FORM INPUT TO $new_post ARRAY
  $new_post = array(
  'post_title'  =>  $title,
  'post_content'  =>  $description,
  'post_category' =>  array($_POST['cat']), // Usable for custom taxonomies too
  'post_status' =>  'publish',           // Choose: publish, preview, future, draft, etc.
  'post_type' =>  'post',  //'post',page' or use a custom post type if you want to
  'genre'  =>  $genre,
  'tranche_age'  =>  $tranche_age,
  );


  //SAVE THE POST
  $pid = wp_insert_post($new_post);


  wp_set_post_terms($pid,array($_POST['reg']),'regions',true);


  //REDIRECT TO THE NEW POST ON SAVE
  $link = site_url();
  wp_redirect( $link );

  //ADD OUR CUSTOM FIELDS
  add_post_meta($pid, 'genre', $genre, true); 
  add_post_meta($pid, 'tranche_age', $tranche_age, true); 


} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');

?>

和表格:

<!-- FRONT END PUBLISHING -->

<div class="wpcf7">
<form id="new_post" name="new_post" method="post" action="" class="wpcf7-form" enctype="multipart/form-data">
  <!-- post name -->
  <fieldset name="name">
    <label for="title">Titre:</label>
    <input type="text" id="title" value="" tabindex="5" name="title" />
  </fieldset>

  <!-- post Category -->
  <fieldset class="category">
    <label for="cat">Catégorie :</label>
    <?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0&show_option_all=Choisissez une catégorie' ); ?>
  </fieldset>

  <!-- post Region -->
  <fieldset class="regions">
    <label for="reg">Régions :</label>
    <?php wp_dropdown_categories( 'tab_index=10&taxonomy=regions&hide_empty=0&show_option_all=Choisissez une Région' ); ?>
  </fieldset>

  <!-- post Content -->
  <fieldset class="content">
    <label for="description">Question :</label>
    <textarea id="description" tabindex="15" name="description" cols="80" rows="10"></textarea>
  </fieldset>

  <!-- Genre -->
  <fieldset class="genre">
    <label for="genre">Genre</label>
    <input type="text" value="" id="genre" tabindex="20" name="genre" />
  </fieldset>

  <!-- Tranche d'âge -->
  <fieldset class="tranche_age">
    <label for="tranche_age">Tranche d'âge</label>
    <input type="text" value="" id="tranche_age" tabindex="20" name="tranche_age" />
  </fieldset>

  <fieldset class="submit">
    <input type="submit" value="Envoyer" tabindex="40" id="submit" name="submit" />
  </fieldset>

  <input type="hidden" name="action" value="new_post" />
  <?php wp_nonce_field( 'new-post' ); ?>
</form>
</div> <!-- END WPCF7 -->

有谁知道我做错了什么?

0 个答案:

没有答案