2014-12-04 4 views
0

저는 주로이 튜토리얼을 기반으로 작업하고 있습니다. 프론트 엔드 형식으로 제출 버튼을 눌렀을 때 게시물 제목을 변경하게하지만 카테고리 업데이트를 허용하고이를 수행하는 방법을 모릅니다.Wordpress의 프론트 엔드에서 카테고리를 업데이트하십시오.

나는 wp_dropdown_categories를 사용하여 기존 범주의 드롭 다운 상자를 열어 "cat"이라는 이름과 ID를 알면 도움이됩니다.

update_post_meta가 관련되어있을 수 있습니다 만, 나는 잘못 될 수 있다고 생각합니다. 어쨌든이를 구현하는 방법에 대해 잘 모릅니다.

<?php 
if(isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) { 

     $postTitle = trim($_POST['title']); 

     $post_id = get_the_ID(); 
     $my_post['ID'] = $post_id; 

     $template_dir = get_bloginfo('template_directory'); 
     $my_post = array(); 
     $my_post['post_status'] = 'publish'; 
     $my_post['post_title'] = $postTitle; 
     $my_post['filter'] = true; 
     wp_update_post($my_post); 

     wp_redirect(get_permalink($post_id)); 
     exit; 
    } 

get_header(); 
?> 


    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

    <form action="" id="update-post" method="post"> 
     <label>Post</label> 
     <input type="text" id="title" name="title" value="<?php echo the_title(); ?>" /> 

     <label>Category</label> 
     <?php $categories = get_the_category(); 
     $category_id = $categories[0]->cat_ID;?> 
     <?php wp_dropdown_categories("show_count=1&hierarchical=1&orderby=name&order=ASC&selected=" . $category_id . "&hide_empty=0&show_option_all=None"); ?> 

     <input type="submit" name="submit" value="Submit" /> 

     <?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?> 
    </form> 

    <?php endwhile; endif; ?> 


<?php get_footer(); ?> 

미리 감사드립니다.

답변

2

wp_set_object_terms()을 사용할 수 있습니다. Codex에 대한 자세한 정보.

wp_set_object_terms($post_id, intval($_POST['cat']), 'category', false); 
+0

감사합니다. 그거 쉽지? – user3256143

관련 문제