2010-11-25 8 views
1

J'ai écrit un script qui prend un flux XML de nouvelles, et insère chaque histoire comme un article dans WordPress en utilisant wp_insert_post().Wordpress - Insertion par programme de messages avec de nouvelles catégories?

Chacune de ces nouvelles a une catégorie que je voudrais ajouter à WordPress aussi.

Ma question est, comment puis-je créer de nouvelles catégories à la volée et affecter le poste à cette catégorie en utilisant wp_insert_post() (ou toute autre fonction)?

Merci!

+0

Je ne connais aucune fonction au sein de WordPress qui fait cela. Cependant, vous pouvez ajouter le titre/id de la catégorie à la table avec un appel personnalisé. – Damien

Répondre

3

Je l'ai triés à la fin en écrivant le code suivant:

//Check if category already exists 
$cat_ID = get_cat_ID($category); 

//If it doesn't exist create new category 
if($cat_ID == 0) { 
    $cat_name = array('cat_name' => $category); 
    wp_insert_category($cat_name); 
} 

//Get ID of category again incase a new one has been created 
$new_cat_ID = get_cat_ID($category); 

// Create post object 
$new_post = array(
    'post_title' => $headline, 
    'post_content' => $body, 
    'post_excerpt' => $excerpt, 
    'post_date' => $date, 
    'post_date_gmt' => $date, 
    'post_status' => 'publish', 
    'post_author' => 1, 
    'post_category' => array($new_cat_ID) 
); 

// Insert the post into the database 
wp_insert_post($new_post); 
1

Essayez ceci:

$my_cat = array('cat_name' => 'My Category', 'category_description' => 'A Cool Category', 'category_nicename' => 'category-slug', 'category_parent' => ''); 

$my_cat_id = wp_insert_category($my_cat); 

$my_post = array(
    'post_title' => 'My post', 
    'post_content' => 'This is my post.', 
    'post_status' => 'publish', 
    'post_author' => 1, 
    'post_category' => array($my_cat_id) 
); 

wp_insert_post($my_post); 

Attention: Untested. Tout a été pris de wp_insert_post() et wp_insert_category() page sur codex.

Questions connexes