【问题标题】:Update category from the front-end in Wordpress从 Wordpress 的前端更新类别
【发布时间】:2015-02-03 00:34:42
【问题描述】:

我主要根据我找到的教程来完成这项工作。这是一个前端表单,让我在点击提交按钮时更改帖子标题,但我也想允许更新类别,但我不知道如何让它这样做。

我正在使用 wp_dropdown_categories 吐出现有类别的下拉框,如果知道它的名称和 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(); ?>

提前致谢!

【问题讨论】:

    标签: wordpress categories frontend


    【解决方案1】:

    您可以使用wp_set_object_terms()。更多信息在Codex

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 2013-02-08
      • 1970-01-01
      相关资源
      最近更新 更多