正确的方法是将数据发布回WordPress admin ajax url,然后在PHP函数中使用$_POST
变量。
将类别id传递回wordpress的jQuery函数示例:
function cat_edit_get() {
jQuery( "#loading-animation").show();
var catID = jQuery("#cat :selected").val();
var text = jQuery("#cat :selected").text();
jQuery(\'#replace\').html(text);
jQuery.ajax({
type: \'POST\',
url: ajaxurl,
data: {"action": "cat-editor-get", cat: catID },
success: function(response) {
jQuery("#the-list").html(response);
jQuery("#loading-animation").hide();
return false;
}
});
}
然后您可以通过挂接到
wp_ajax
行动这里我只传递从下拉选择中选择的类别id:
add_action ( \'wp_ajax_cat-editor-get\', \'cat_editor_get\' );
function cat_editor_get () {
$cat_id = $_POST[ \'cat\' ];
$preview_cats = get_post_meta ( 82799, \'_\' . $cat_id . \'saved\', true );
$item_order = $preview_cats[ \'cat_order\' ];
$post_ids = explode ( ",", $item_order, 30 );
global $post;
if ( count ( $post_ids ) < 5 ) {
$args = array (
\'category_id\' => $cat_id,
\'length\' => 30,
);
$post_ids = wndsq_get_post_ids ( $args );
}