在快速编辑中显示菜单顺序字段

时间:2021-04-08 作者:Zaheer Abbas

我在快速编辑中使用ACF创建了一个自定义字段。一个字段显示用户必须更新产品菜单顺序的位置。单击“编辑”按钮查看“产品详细信息”页面时,该字段将显示在“高级”选项卡“菜单顺序”中。现在,用户希望更改快速编辑字段中的菜单顺序。我正在使用ajax来实现,但我被困了4个小时。我在网上找不到任何帮助。检查下面的我的代码。感谢

JQuery代码:

jQuery(document).ready(function(){

jQuery(".acf-quick-edit input[type=\'text\']").blur(function(){
    var morder = jQuery(this).val();
    var post_id = jQuery(this).parents("tr").attr("id");

    var data = {
        \'action\': \'update_menu_order\',
        \'menu_order\': morder,
        \'post_id\':post_id
    };

    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    jQuery.post(ajaxurl, data, function(response) {
        alert(response);
    });


});

});
PHP代码

<?php 

add_action( \'wp_ajax_update_menu_order\', \'update_menu_order\' );
add_action( \'wp_ajax_nopriv_update_menu_order\', \'update_menu_order\' );

function update_menu_order() {
    global $wpdb; // this is how you get access to the database

    $menu_order = $_POST[\'menu_order\'];
    $data = $_POST[\'post_id\'];
    $post_id = substr($data, strpos($data, "-") + 1);    
//print_r($_POST);
//echo get_post_field( \'menu_order\', $post_id);
update_post_meta($post_id,\'menu_order\',$menu_order);

    

    wp_die(); // this is required to terminate immediately and return a proper response
}

1 个回复
SO网友:Zaheer Abbas

下面的代码将帮助其他人完成上述任务。

<?php 
add_action( \'wp_ajax_update_menu_order\', \'update_menu_order\' );
add_action( \'wp_ajax_nopriv_update_menu_order\', \'update_menu_order\' );

function update_menu_order() {
global $wpdb; 
$table_name = $wpdb->prefix."posts";

$menu_order = $_POST[\'menu_order\'];
$data = $_POST[\'post_id\'];
$post_id = substr($data, strpos($data, "-") + 1);    

$result = $wpdb->query($wpdb->prepare("UPDATE $table_name SET menu_order=\'$menu_order\' WHERE ID=$post_id"));

echo $result;


wp_die(); // this is required to terminate immediately and return a proper response
}

相关推荐