2017-07-22 12 views
1

私はwordpressの

    <?php 
        $include = array(); 
        $categories = get_terms('category', array(
         'include' => $include, 
         'hide_empty' => false, 
        )); 

        $categories_count = count($categories); 
        if ($categories_count > 1) : 
        ?> 
        <div class="form-categories"> 
         <ul> 
          <?php 
          foreach ($cats as $cat) { 
           echo '<li class="form-categories-item"><input type="checkbox" id="post_cat-' . esc_attr($cat->term_id) . '" name="post_category[]" value="' . esc_attr($cat->term_id) . '" /><label for="post_cat-' . esc_attr($cat->term_id) . '">' . esc_attr($cat->name) . '</label></li>'; 
          } 
          ?> 
         </ul> 
        </div> 
       <?php endif; ?> 

は、どのように私は追加することができ、このコードですべてのカテゴリを表示することができ、ワードプレスでポスト編集ページのフロントエンドを作成しようとしている中カテゴリのフロントエンドを更新する方法タグをチェックします投稿を作成するときに選択したカテゴリに

おかげ

+0

Read:https://stackoverflow.com/questions/27305740/update-category-from-the-front-end-in-wordpress –

答えて

1

あなたは以下のコードを試すことができます。

<?php 
    $include = array(); 
    $categories = get_terms('category', array(
     'include' => $include, 
     'hide_empty' => false, 
    )); 

    $categories_count = count($categories); 

    // get post categories 
    $post_cats  = get_the_terms(get_the_ID(), 'category'); 
    $post_cats_arr = array(); 

    foreach ($post_cats as $post_cat) 
    { 
     $post_cats_arr[] = $post_cat->term_id; 
    } 

    if ($categories_count > 1) : 
?> 
    <div class="form-categories"> 
     <ul> 
      <?php 
       foreach ($categories as $cat) 
       { 
        $checked = ''; 

        if (in_array($cat->term_id, $post_cats_arr)) 
        { 
         $checked = 'checked'; 
        } 

        echo '<li class="form-categories-item"><input type="checkbox" checked="' . $checked . '" id="post_cat-' . esc_attr($cat->term_id) . '" name="post_category[]" value="' . esc_attr($cat->term_id) . '" /><label for="post_cat-' . esc_attr($cat->term_id) . '">' . esc_attr($cat->name) . '</label></li>'; 
       } 
      ?> 
     </ul> 
    </div> 

<?php endif; ?> 

感謝を!

+1

ありがとうございました –

+0

ようこそ。 – Spartan