我有一个ajax函数,可以通过ajax将用户帖子的视图类型从列表模式切换到网格模式,但问题是,当调用wp查询时,它无法获取正在查看的用户配置文件的id。当我手动输入id号时,它会起作用,但当我尝试使用$user->id或$user\\u id或$user[\'id]时,它不会起作用。有人能帮我弄清楚吗?
这是我当前的php函数,它是在我的函数中编写的。php文件:
function profile_view(){
$args = array(
\'post_type\' => \'listings\',
\'post_status\' => \'publish\',
\'meta_query\' => array(
array(
\'key\' => \'stm_car_user\',
\'value\' => $user->ID
)
)
);
$query = new WP_Query($args);
//$query = stm_user_listings_query(\'1\', \'publish\');
$response = array();
//Grid/list settings
$view_list = \'\';
$view_grid = \'\';
$view_map = \'\';
$current_link_args = array();
if(!empty($_GET)){
$current_link_args = $_GET;
}
$view_list_link = $view_grid_link = $view_map_link = $current_link_args;
$view_list_link[\'view_type\'] = \'list\';
$view_grid_link[\'view_type\'] = \'grid\';
$view_map_link[\'view_type\'] = \'map\';
if(!empty($_GET[\'view_type\'])) {
if ( $_GET[\'view_type\'] == \'list\' ) {
$view_list = \'active\';
} elseif ( $_GET[\'view_type\'] == \'grid\' ) {
$view_grid = \'active\';
$current_link_args[\'view_type\'] = \'grid\';
}
elseif ( $_GET[\'view_type\'] == \'map\' ) {
$view_map = \'active\';
$current_link_args[\'view_type\'] = \'map\';
}
} else {
$view_list = \'active\';
}
if ( $query->have_posts() ):
ob_start();
$template = \'partials/listing-cars/listing-list-directory-loop\';
if(!empty($_GET[\'view_type\']) and $_GET[\'view_type\'] == \'grid\') {
$template = \'partials/listing-cars/listing-grid-directory-loop\';
} elseif(!empty($_GET[\'view_type\']) and $_GET[\'view_type\'] == \'map\') {
$template = \'partials/listing-cars/listing-map\';
} else {
$template = \'partials/listing-cars/listing-list-directory-loop\';
}
while ( $query->have_posts() ) {
$query->the_post();
get_template_part($template);
}
$response[\'html\'] = ob_get_contents();
ob_end_clean();
else:
endif;
wp_reset_postdata();
$show_pagination = true;
if(!empty($query->found_posts) and !empty($query->query_vars[\'posts_per_page\'])) {
if($query->found_posts < $query->query_vars[\'posts_per_page\']) {
$show_pagination = false;
}
}
$response = json_encode( $response );
echo $response;
exit;
}
add_action(\'wp_ajax_profile_view\' , \'profile_view\');
add_action(\'wp_ajax_nopriv_profile_view\',\'profile_view\');
我也不想使用wp\\u get\\u current\\u user(),因为它只显示当前登录用户的帖子,我希望在查看某个用户的个人资料时,即使我没有登录,也能看到该用户的所有帖子。。。
以下是ajax函数:
$(\'.stm-view-by-profile a:not(.view-map)\').on(\'click\', function(e){
e.preventDefault();
var viewType = $(this).data(\'view\');
$(\'#stm_view_type_profile\').val(viewType);
$(\'.stm-view-by-profile a\').removeClass(\'active\');
$(this).addClass(\'active\');
var data_form = $(\'.user_posts\').serialize();
$.ajax({
url: ajaxurl,
dataType: \'json\',
context: this,
data: data_form + \'&action=profile_view\',
beforeSend: function(){
$(\'.stm-ajax-row\').addClass(\'stm-loading\');
},
success: function (data) {
console.log(data);
$(\'.stm-isotope-sorting\').html(data.html);
$(\'.stm-ajax-row\').removeClass(\'stm-loading\');
}
});
});
最合适的回答,由SO网友:IvanMunoz 整理而成
EDIT:如果需要执行以下操作,则需要从AJAX获取变量:
function profile_view() {
$user_id = $_GET["user_id"]; // name of hidden input
$args = array(
\'post_type\' => \'listings\',
\'post_status\' => \'publish\',
\'meta_query\' => array(
array(
\'key\' => \'stm_car_user\',
\'value\' => $user_id
)
)
);
...