Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress Ajax请求,未加载正确的类别**_Php_Ajax_Wordpress - Fatal编程技术网

Php WordPress Ajax请求,未加载正确的类别**

Php WordPress Ajax请求,未加载正确的类别**,php,ajax,wordpress,Php,Ajax,Wordpress,我使用以下命令发出ajax请求,它不是只返回与特定类别相关的内容,而是返回所有可用的帖子**(在归档/类别页面上运行此加载)。我应该在这个特定的类别页面上加载id为“3”的类别 任何提示或技巧都将不胜感激 $('#load-more').click(function (e) { e.preventDefault(); var pageCount = $(this).attr('data-page'); var nextPage = parseInt($(this).att

我使用以下命令发出ajax请求,它不是只返回与特定类别相关的内容,而是返回所有可用的帖子**(在归档/类别页面上运行此加载)。我应该在这个特定的类别页面上加载id为“3”的类别

任何提示或技巧都将不胜感激

$('#load-more').click(function (e) {
    e.preventDefault();
    var pageCount = $(this).attr('data-page');
    var nextPage = parseInt($(this).attr('data-page')) + 1;
    var catID = $(this).attr('data-cat');
    console.log(catID);
    if ($(this).hasClass('shake')) {
        (this).removeClass('shake');
    }
    $.ajax({
        dataType: 'JSON',
        url: ajaxurl,
        data: {
            'action': 'more_posts',
            'pageCount': pageCount,
            'cat': catID,
        },
        success: function (data) {
            console.log(data);
            console.log(data.message);
            if (data.message === "nomore") {
                console.log('none');
            } else {
                $('#appended-content').append(data.message);
            }
        },
        complete: function (data) {
            $('#load-more').attr('data-page', nextPage);
            console.log(data);
            console.log(data.message);
        }
    });
});  
如果我手动将其设置为3,它会工作,因此如果我不得不假设,我没有在“args”中正确获取和/或设置id

add_action('wp_ajax_more_posts', __NAMESPACE__ . '\\ajax_more_posts');
add_action('wp_ajax_nopriv_more_posts', __NAMESPACE__ . '\\ajax_more_posts');
function ajax_more_posts()
{
    $cat_id = $_POST['cat'];  //not sure if this is actually getting the category
    $paged = $_REQUEST['pageCount'];
    $cat = $_REQUEST['dataCat'];
    ob_start();
    $args = [
        'cat' => $cat_id, //if I set to 3 manually it works
        'posts_per_page' => 4,
        'post_status' => 'publish'
    ];
    if ($paged != "") {
        $args['paged'] = $paged;
    }
    // if($cat != ""){
    //     $args['cat'] = $cat;
    // }
    $moreBlogPosts = new WP_Query($args);
    $html = '';
    if ($moreBlogPosts->have_posts()) {
        while ($moreBlogPosts->have_posts()) : $moreBlogPosts->the_post();
            $id = get_the_ID();
            $cats = get_the_category($id);
            echo '<div class="post-preview">';
            include(locate_template('partials/posts/preview-components/preview-block.php', false, false));
            get_template_part('partials/posts/preview-components/shopstyle-carousel');
            echo '</div>';
        endwhile;
    } else {
        echo "nomore";
    }
    wp_reset_query();
    wp_reset_postdata();
    $message = ob_get_clean();
    echo json_encode(['message' => $message]);
    die();
}
add_action('wp_-ajax_-more_-posts'、\uu-NAMESPACE_.'\\ajax_-more-posts');
添加操作('wp\u ajax\u nopriv\u more\u posts',\u名称空间\u.'\\ajax\u more\u posts');
函数ajax\u more\u posts()
{
$cat\u id=$\u POST['cat'];//不确定这是否真的得到了类别
$paged=$_请求['pageCount'];
$cat=$_请求['dataCat'];
ob_start();
$args=[
'cat'=>$cat_id,//如果我手动设置为3,它会工作
“每页帖子数”=>4,
“发布状态”=>“发布”
];
如果($paged!=“”){
$args['paged']=$paged;
}
//如果($cat!=“”){
//$args['cat']=$cat;
// }
$moreBlogPosts=新的WP_查询($args);
$html='';
如果($moreBlogPosts->have_posts()){
而($moreBlogPosts->have_posts()):$moreBlogPosts->the_post();
$id=get_the_id();
$cats=获取类别($id);
回声';
包括(定位_模板('partials/posts/preview components/preview block.php',false,false));
获取模板(partials/posts/preview components/shopstyle carousel);
回声';
结束时;
}否则{
呼应“nomore”;
}
wp_reset_query();
wp_reset_postdata();
$message=ob_get_clean();
echo json_编码(['message'=>$message]);
模具();
}

您尚未在Ajax调用中指定方法

$.ajax({
    dataType: 'JSON',
    url: targetUrl,
    success: function(data){

    },
    complete: function (data) {

    }
});
因此,默认情况下,请求以GET方法发送。这就是背后的原因

有两种方法可以解决这个问题

方法1:在Ajax调用中添加方法

方法2:更改服务器端脚本以接受GET请求(您可以使用$\u GET或$\u request)


示例:$\u REQUEST['cat']或$\u GET['cat']

在这种情况下,如果您可以使用$.post而不是$.ajax,那就好了

var data={
          'action': 'more_posts',
          'pageCount': pageCount,
          'cat': catID,
}
$.post(ajaxurl, data,function(res){
    var json_obj = JSON.parse(res);     
    $('#appended-content').append(json_obj.message);
    //your code here        
});

希望能有所帮助。

你能给我一个例子的链接吗?@AhmedGinani抱歉,如果你得到的是正确的catID,那么这都是本地检查和警报:警报(catID)@AhmedGinani catID正确,警报和控制台正确尝试将$_POST['cat']更改为$_REQUEST['cat'],也尝试像cat一样传递它:parseInt(catID),这非常有效。到底是什么原因让它工作的?
var data={
          'action': 'more_posts',
          'pageCount': pageCount,
          'cat': catID,
}
$.post(ajaxurl, data,function(res){
    var json_obj = JSON.parse(res);     
    $('#appended-content').append(json_obj.message);
    //your code here        
});