我正在创建一个wordpress主题,并具有一个称为模板部分的页面模板设置。该模板部分运行查询以加载某些帖子类型。首次加载页面时,将从主模板传递查询参数。当按下按钮时,将调用ajax函数,该函数再次设置参数(使用相同的变量名)并重新运行模板部分。
主模板文件:
get_header();
$venue_search_args = array(
'post_type' => 'tribe_venue', //set the arguments for the search query here.
'post_status' => 'publish',
);
?>
<div id ="venue-filter-form">
<form id="venue-search" action="venue-search.php" method="post">
//search form here
</form>
</div>
<?php
include(locate_template('/template-parts/template-venue-part-1.php')); // include the post content ( this includes the search query)
get_footer();
?>
上面的代码调用了以下模板零件文件,该文件包含一个查询以搜索所有帖子类型并将其显示在div中,其中class =类别名称。
$query = new WP_Query($venue_search_args);
while ($query->have_posts()) { ?>
<div class=" venue <?php
$formatted_cats = format_category_output(get_the_taxonomies(get_the_ID())); //return an array of formatted categories the value sent to the function is an array of unformatted categories for the post
if (is_array($formatted_cats)){
$i=0;
$len = count($formatted_cats);
foreach ($formatted_cats as $key ) {
if ($i == $len-1){
echo $key ;
}
else {
echo $key;
echo ' ';
}
$i++;
}
}
else { echo 'uncategorised'; }
?>">
下面是我当前未完成的ajax调用,我正在使用它们来测试:这将从div中删除所有包含我所有帖子的内容,使我留有空白页。
jQuery(document).ready(function () {
jQuery("#venue-search").submit(function(event) {
event.preventDefault();
jQuery("#venues-to-show").empty(); // stop form submit
//get the value in the drop down
var category = jQuery("#venue-search-category").val();
var data = {
action: 'venue_search',
category: category
};
jQuery.post(venue_searchAjax.ajaxurl, data, function(response) {
//alert('got this from the server: ' + response) ;
});
});
});
Ajax请求发送到包含以下功能的文件。调用的函数尝试设置一些新的查询参数,并且调用与以前相同的模板部分。最终,我将解析结果以重新构成页面,仅显示具有搜索类别的帖子。
function venue_search() {
$category = $_POST['category'];
$venue_search_args = array(
'post_type' => 'tribe_venue',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tribe_events_cat',
'field' => 'slug',
'terms' => $category
),
));
get_template_part( '/template-parts/template-venue-part-1');
die();
}
我想知道变量$ venue_search_args将如何工作?我应该将其设置为全局/会话,以便即时访问/更改同一数据库的值吗?
我不太了解它的行为。
get_template_part()
充当php require()
,这意味着该行实际上已被导入文件中找到的代码替换。因此,如果在导入之前创建变量,则可以按要在文件中使用的方式使用它。如果在函数内部导入,则在导入的代码内创建的变量将是局部变量,该代码仅插入进行导入的位置。
$venue_search_args
当然,使全局变量能够从外部进行修改是唯一可能的事情,那就是在导入的代码所在的位置定义变量。
如果您不需要从外部修改它,而只希望每次调用都可以修改这些参数,则可以在中添加一个参数data
,然后将其作为POST
内部的值检索venue_search
,方法与之相同。做完了category
。然后,您可以为每个搜索更改此变量,而无需使其成为全局变量:
var data = {
action: 'venue_search',
category: category,
posttype: 'tribe_venue' //example with post type
};
然后:
function venue_search() {
$category = $_POST['category'];
$posttype = $_POST['posttype']; //get the value that changes $venue_search_args
$venue_search_args = array(
'post_type' => $posttype,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tribe_events_cat',
'field' => 'slug',
'terms' => $category
),
));
get_template_part( '/template-parts/template-venue-part-1');
die();
}
希望这就是您想要的,如果没有,请发表评论,我会帮助的:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句