当我在先前定义的通过ajax调用的函数中使用相同的变量名时会发生什么?

丹·巴尔

我正在创建一个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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当我调用“ BufferStrategy.show()”时会发生什么?

来自分类Dev

当我多次调用requestAnimationFrame时会发生什么

来自分类Dev

当我调用“ BufferStrategy.show()”时会发生什么?

来自分类Dev

当我在嵌套的 React 组件中调用作为 props 传递的函数时会发生什么?

来自分类Dev

当我使用多个模拟时间时会发生什么?

来自分类Dev

当我截断正在使用的文件时会发生什么?

来自分类Dev

当我使用多个模拟时间时会发生什么?

来自分类Dev

当我在Bash脚本中对函数使用`&`时会发生什么?

来自分类Dev

当我将变量'x'更改为'fab'时会发生什么?关于装饰器并传递函数变量

来自分类Dev

当我以`even`作为生成器函数调用`even(3)`时会发生什么情况?

来自分类Dev

定义函数时会发生什么?

来自分类Dev

如果我在fortran中调用一个函数而未定义变量,会发生什么?

来自分类Dev

当我在C ++中对未初始化的指针调用“删除”时会发生什么?

来自分类Dev

当我在console.log中调用console.log时会发生什么?

来自分类Dev

当我通过 systemctl 或 init 重新启动服务时会发生什么

来自分类Dev

新手Q-当我在printf中遗漏变量时会发生什么

来自分类Dev

当我在函数中重新分配可变默认参数时会发生什么?

来自分类Dev

当我在此函数调用中使用括号使它的行为有所不同时,会发生什么情况?

来自分类Dev

当我绑定到AliasProperty时会发生什么?

来自分类Dev

当我放弃对iOS 6的支持时会发生什么?

来自分类Dev

MySql-当我用“ and”更新时会发生什么

来自分类Dev

当我放弃对iOS 6的支持时会发生什么?

来自分类Dev

当我将%esp移至%ebp时会发生什么?

来自分类Dev

当我不输入期望的命令时会发生什么?

来自分类Dev

当我忘记登录TTY时会发生什么?

来自分类Dev

当我取消订阅 Observable 链时会发生什么?

来自分类Dev

为什么我们在python中使用与namedtuple的类型名相同的变量名?

来自分类Dev

当我使用void函数的返回值(通过强制转换函数指针)时,会发生什么?

来自分类Dev

当我混合使用有符号和无符号类型时会发生什么?

Related 相关文章

  1. 1

    当我调用“ BufferStrategy.show()”时会发生什么?

  2. 2

    当我多次调用requestAnimationFrame时会发生什么

  3. 3

    当我调用“ BufferStrategy.show()”时会发生什么?

  4. 4

    当我在嵌套的 React 组件中调用作为 props 传递的函数时会发生什么?

  5. 5

    当我使用多个模拟时间时会发生什么?

  6. 6

    当我截断正在使用的文件时会发生什么?

  7. 7

    当我使用多个模拟时间时会发生什么?

  8. 8

    当我在Bash脚本中对函数使用`&`时会发生什么?

  9. 9

    当我将变量'x'更改为'fab'时会发生什么?关于装饰器并传递函数变量

  10. 10

    当我以`even`作为生成器函数调用`even(3)`时会发生什么情况?

  11. 11

    定义函数时会发生什么?

  12. 12

    如果我在fortran中调用一个函数而未定义变量,会发生什么?

  13. 13

    当我在C ++中对未初始化的指针调用“删除”时会发生什么?

  14. 14

    当我在console.log中调用console.log时会发生什么?

  15. 15

    当我通过 systemctl 或 init 重新启动服务时会发生什么

  16. 16

    新手Q-当我在printf中遗漏变量时会发生什么

  17. 17

    当我在函数中重新分配可变默认参数时会发生什么?

  18. 18

    当我在此函数调用中使用括号使它的行为有所不同时,会发生什么情况?

  19. 19

    当我绑定到AliasProperty时会发生什么?

  20. 20

    当我放弃对iOS 6的支持时会发生什么?

  21. 21

    MySql-当我用“ and”更新时会发生什么

  22. 22

    当我放弃对iOS 6的支持时会发生什么?

  23. 23

    当我将%esp移至%ebp时会发生什么?

  24. 24

    当我不输入期望的命令时会发生什么?

  25. 25

    当我忘记登录TTY时会发生什么?

  26. 26

    当我取消订阅 Observable 链时会发生什么?

  27. 27

    为什么我们在python中使用与namedtuple的类型名相同的变量名?

  28. 28

    当我使用void函数的返回值(通过强制转换函数指针)时,会发生什么?

  29. 29

    当我混合使用有符号和无符号类型时会发生什么?

热门标签

归档