douchiwan1503 2017-12-05 20:40
浏览 103
已采纳

来自锚点击的wp_update_post类别

Would like to change and update a post category using wp_update_post. The category should be changed by clicking <a> or <button>

As far as I can see, this should apply the update to the post;

$live_paused = array(
'post_category' => 6
  );

// Update the post into the database
  wp_update_post( $live_paused );

But how can I turn add this function to this

 echo '<a href="" id=""><i class="fa fa-pause"></i></a>';

Edit - further information

Update Post Code in functions of theme - not tested yet.

function live_paused_status( $post_id ){
if (current_user_can('edit_post', $post->ID)) {

    $live_paused = array(
        'post_category' => 6
    );
    echo '<a href="" id=""><button title="" data-toggle="tooltip" class="campaigns-link-button" type="button" data-original-title="Pause Campaign"><i class="fa fa-pause"></i></button></a>';

    // unhook this function so it doesn't loop infinitely
    remove_action('save_post', 'live_paused_status');

    // update the post, which calls save_post again
    wp_update_post( $live_paused );

    // re-hook this function
    add_action('save_post', 'live_paused_status');
 }
}

add_action('save_post', 'live_paused_status');

Loop

 <?php $query = new WP_Query( array( 'post_type' => 'campaigns'));?>
    <?php if ($query->have_posts()) : while ( $query->have_posts() ) : $query->the_post(); ?>
      <div class="card">
        <div class="card-footer">
          <div class="row">
            <div class="col-4 campaigns-link">
              <?php echo live_paused_status(); ?>
            </div>
          </div>
        </div>
      </div>
   <?php endwhile; wp_reset_postdata(); ?>

展开全部

  • 写回答

1条回答 默认 最新

  • dpp3047 2017-12-05 21:24
    关注

    You need to make an AJAX request to a script that you hook up. Hooking up AJAX in WordPress is a little weird, but rather than try to do it for you, you should look at the documentation in the WordPress Codex:

    https://codex.wordpress.org/AJAX_in_Plugins

    You are going to add an action that will basically be passed the post category ID from a form input, you will use JS to send that in a POST request, and then you will grab that ID and update the category.

    Hope this helps.

    EDIT

    <?php 
    
    add_action( 'wp_ajax_custom_update_category', 'custom_update_category' );
    
    function custom_update_category() {
        $cat_id = sanitize_text_field( $_POST['cat_id']); //passed from AJAX. Make sure to escape it just in case.
        # update the category here with $cat_id
    }
    

    To try to refine my answer even more, without writing it for you:

    1. Create your form. I imagine this having some type of select input with the name="cat_id", and then the value of each option as the $cat_id in a loop in the WP template
    2. You need to create a POST request with AJAX containing a cat_id parameter (pulls this from the select input's value). Here's one I have made in the past.

            var cat_id = $('#product-cat-select').val();
      
            $.ajax({
              type : "POST",
              url : ajaxurl,
              data : {
                action: "custom_update_category",
                cat_id: cat_id
              },
              success: function(response) {
                console.log( response );
              }
           });
      
    3. Notice that I named the action the same thing as the suffix on the add_action in my example (custom_update_category). The cat_id is being grabbed from the select input and then sent up in the data object of the AJAX request.

    4. Using that data object, I can grab the $cat_id within my PHP within that script.

    That action name is really the key.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部