Domanda

questo codice funziona bene. Il problema che ho è che quando mi presento si controlla solo la categoria padre non la sottocategoria anche se ho specificato sia un genitore e una sottocategoria.

per favore. Che cosa sto facendo di sbagliato. ohh se si guarda nel pezzo html, il 7 è gatto genitore ed i numeri dopo la virgola sono le sottocategorie.

   <?php 
if(isset($_POST['new_post']) == '1') {
    $post_title = $_POST['post_title'];
    $post_category = $_POST['cat'];
    $post_content = $_POST['post_content'];
    $new_post = array(
          'ID' => '',
          'post_author' => $user->ID, 
          'post_content' => $post_content,
          'post_title' => $post_title,
          'post_status' => 'publish',
          'post_category' => array($post_category)

        );

    $post_id = wp_insert_post($new_post);

    // This will redirect you to the newly created post
    $post = get_post($post_id);
    wp_redirect($post->guid);
}      
?>

Questo è il html

<form method="post" action="" name="" onsubmit="return checkformf(this);">


<input type="text" name="post_title" size="45" id="input-title"/>

<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea></br>


<ul><select name='cat' id='cat' class='postform' > 
    <option class="level-0" value="7,95">child cat1</option> 
    <option class="level-0" value="7,100">child cat2</option> 
    <option class="level-0" value="7,101">child cat3</option> 
    <option class="level-0" value="7,94">child cat4</option> 
</select> 
</ul>

<input type="hidden" name="new_post" value="1"/> 

<input class="subput" type="submit" name="submitpost" value="Post"/> 


</form>

per favore fatemelo sapere se avete bisogno di ulteriori informazioni. grazie in anticipo

È stato utile?

Soluzione

Il problema è, non si può fare una serie del genere in PHP. Cercando di lanciare una stringa che contiene un elenco separato da virgole come un array solo produce un array con un singolo valore - la vostra separati da virgola stringa

.

Si desidera utilizzare php di esplodere funzione per creare l'array. Ci vuole una stringa un divide in una matrice in buona fede sulla base di un delimitatore arbitrario (nel tuo caso, useremo una virgola).

provare qualcosa di simile:

if(isset($_POST['new_post']) == '1') {
    $post_title = $_POST['post_title'];
    $arr_post_category = explode(',',$_POST['cat']); // EXPLODE!
    $post_content = $_POST['post_content'];
    $new_post = array(
          'ID' => '',
          'post_author' => $user->ID, 
          'post_content' => $post_content,
          'post_title' => $post_title,
          'post_status' => 'publish',
          'post_category' => $arr_post_category // NOW IT'S ALREADY AN ARRAY

        );

    $post_id = wp_insert_post($new_post);

    // This will redirect you to the newly created post
    $post = get_post($post_id);
    wp_redirect($post->guid);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top