Php 如何在wordpress中更新类别前端

Php 如何在wordpress中更新类别前端,php,wordpress,Php,Wordpress,我试图在wordpress中使后期编辑页面成为前端,我可以用此代码显示所有类别 <?php $include = array(); $categories = get_terms('category', array( 'include' => $include, 'hid

我试图在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; ?> 

创建帖子时,如何将选中的标签添加到选定类别


谢谢

您可以尝试以下代码:

<?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; ?> 

谢谢

阅读: