douchiwan1503 2017-12-06 04: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-06 05: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.

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

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂