is_page()条件在AJAX函数内部不起作用



我正在使用ajax过滤帖子。

我希望$array对于页面";"产品";以及";Faq";

如果这是在ajax请求中执行的,则不会设置is_page((。您请求的页面是以前的请求。您需要在原始页面上设置一些内容,以便传递ajax请求,让它知道它是为您的特殊情况页面准备的。

functions.php

function load_scripts() {

wp_enqueue_script( 'ajax', get_template_directory_uri() . '/vendor/jquery/main.js', array( 'jquery' ), NULL, true );

wp_localize_script( 'ajax', 'wp_ajax',
array( 
'ajax_url' => admin_url( 'admin-ajax.php' ),
'is_page'  => is_page( 420 ), // Whatever you want to pass. Add as many key pairs as needed.
)
);

}
add_action( 'wp_enqueue_scripts', 'load_scripts');
add_action( 'wp_ajax_nopriv_filter', 'filter_ajax' );
add_action( 'wp_ajax_filter', 'filter_ajax' );

function filter_ajax() {

$category = $_POST['category'];
if(isset($_POST['is_page']) && $_POST['is_page'] === 'faq'){
$cat__in = 10;
print_r(  $_POST );
echo "FAQ";
} else{
$cat__in = 4;
print_r(  $_POST );
echo "Products";
}

$args = array(
'post_type' => 'post',
'posts_per_page' => 50,
'category__in' => $cat__in,
);
if(isset($category)) {
$args['category__in'] = array($category);
}

$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
the_title('<h2>', '</h2>');
the_content('<p>', '</p>');
endwhile;
endif;
wp_reset_postdata(); 
die();
}

faq.php

<div class="js-filter">
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 100,
'child_of' => 10,
'exclude' => array(1),
);
$query = new WP_Query(array( 'cat' => 10 ));
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
the_title('<h2>', '</h2>');
the_content('<p>', '</p>');
endwhile;
endif;
wp_reset_postdata(); ?>
</div>
<div class="categories">
<ul>
<?php 
$cat_args = array(
'exclude' => array(1),
'child_of' => 10
);
?>
<?php 
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>
<li class="js-filter-item"><a href="#">All</a></li>
<?php $categories = get_categories($cat_args); foreach($categories as $cat) : ?>
<li class="js-filter-item"><a data-category="<?= $cat->term_id; ?>" href="<?= get_category_link($cat->term_id); ?>"><?= $cat->name; ?></a></li>
<?php endforeach; ?>
</ul>
</div>

products.php

<div class="js-filter">
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 100,
'child_of' => 6,
'exclude' => array(1,10),
);
$query = new WP_Query(array( 'cat' => 6 ));
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
the_title('<h2>', '</h2>');
the_content('<p>', '</p>');
endwhile;
endif;
wp_reset_postdata(); ?>
</div>
<div class="categories">
<ul>
<?php 
$cat_args = array(
'exclude' => array(1,10),
'child_of' => 6
);
?>
<?php 
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>
<li class="js-filter-item"><a href="#">All</a></li>
<?php $categories = get_categories($cat_args); foreach($categories as $cat) : ?>
<li class="js-filter-item"><a data-category="<?= $cat->term_id; ?>" href="<?= get_category_link($cat->term_id); ?>"><?= $cat->name; ?></a></li>
<?php endforeach; ?>
</ul>
</div>

main.js

jQuery(function($){
$(document).on('click', '.js-filter-item > a', function(e){
e.preventDefault();
var category = $(this).data('category');
$.ajax({
url: wp_ajax.ajax_url,
data: {
action: 'filter',
is_page: wp_ajax.is_page, // This param will be passed to your ajax function.
category: category,
pageslug: location.pathname.replace(///g,'')
},
type: 'post',
success: function(result) {
$('.js-filter').html(result);
},
error: function(result) {
console.warn(result);
}
});
});
});

你知道怎么解决吗?在点击";所有";按钮,它显示来自";ID=6";类别

您可以使用本地化脚本功能传递参数。由于页面没有传递给ajax函数,因此需要在本地化脚本中设置params,然后作为本地化数组的一部分传递给ajax。

function load_scripts() {

wp_enqueue_script( 'ajax', get_template_directory_uri() . '/vendor/jquery/main.js', array( 'jquery' ), NULL, true );

wp_localize_script( 'ajax', 'wp_ajax',
array( 
'ajax_url' => admin_url( 'admin-ajax.php' ),
'is_page'  => is_page( 420 ), // Whatever you want to pass. Add as many key pairs as needed.
)
);

}
add_action( 'wp_enqueue_scripts', 'load_scripts');

在上面的例子中,is_page要么为true,要么为false,并将通过本地化脚本传递给您的javascript。因此,在JS文件中,wp_ajax.is_page将为true或false(您可以通过检查页面上的JS对象来查看这一点。它应该是0或1。

如果你想传递其他对象,你可以创建尽可能多的项目到你的数组,然后传递到你的js模板。

您的AJAX:

jQuery(function($){
$(document).on('click', '.js-filter-item > a', function(e){
e.preventDefault();
var category = $(this).data('category');
$.ajax({
url: wp_ajax.ajax_url,
data: {
action: 'filter',
is_page: wp_ajax.is_page, // This param will be passed to your ajax function.
category: category,
pageslug: location.pathname.replace(///g,'')
},
type: 'post',
success: function(result) {
$('.js-filter').html(result);
},
error: function(result) {
console.warn(result);
}
});
});
});

然后在您更新的php-ajax操作中。

function filter_ajax() {

$category = $_POST['category'];
// If it's page 420... then $_POST['is_page'] will equal a string 1.
if ( isset( $_POST['is_page'] ) && $_POST['is_page'] === '1' ) {
$cat__in = 10;
print_r( $_POST );
echo "FAQ";

} else {
$cat__in = 4;
print_r( $_POST );
echo "Products";
}
// .... rest of function
}

is_page((不应该在wp_ajax_函数中工作。检查这个答案:为什么is_page(id(在functions.php中不起作用?

is_page((依赖于一个完整的全局$wp_query对象。如果在触发动作template_redirect之前调用它,则可能无法获取该数据。

您可以将slug作为ajax数据数组值传递,而不是使用is_page((。

要以段塞形式传递,请编辑main.js以替换

data: { action: 'filter', category: category },

带有

data: { 
action: 'filter', 
category: category, 
pageslug: location.pathname.replace(///g,'') 
},

然后,在filter_ajax((函数中的functions.php中,替换

if (is_page('faq'))

带有

if(isset($_POST['pageslug']) && $_POST['pageslug'] === 'faq')

最新更新