简体   繁体   English

将变量传递给wordpress函数并使用ajax刷新它

[英]Pass variable to wordpress function and refresh it using ajax

I'm building a shopping page that list all the stores. 我正在建立一个列出所有商店的购物页面。 I've created a sidebar with all the categories and a list with pagination. 我创建了一个包含所有类别的边栏和一个带有分页的列表。 This list is generated by a wordpress function in my theme-functions.php , but when I click in any category, I'm redirected to another page (to http://<domain>/store from http://<domain>/category ). 由我的一个WordPress函数生成的列表theme-functions.php ,但是当我在任何类别的点击,我重定向到另一个页面(到http://<domain>/store来自http://<domain>/category )。 So I created a JavaScript function to prevent the link action using preventDefault and get his href value and set it as variable to pass it to the function via Ajax. 因此,我创建了一个JavaScript函数来防止使用preventDefault的链接操作,并获取他的href值并将其设置为变量,以将其通过Ajax传递给该函数。

My question is: there is a way to pass this variable and refresh the wordpress function on the same page to show this list? 我的问题是:有一种方法可以传递此变量并刷新同一页面上的wordpress函数以显示此列表?

Code below: 代码如下:

Wordpress theme-function.php Wordpress theme-function.php

function storeSingle() {

    $catUrl = $_POST['catUrl'];

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

    $args = array(
        'post_type'             =>  'lojas' ,
        'posts_per_page'        =>  5,
        'orderby'               =>  'title',
        'order'                 =>  'ASC',
        'paged'                 =>  $paged,
        'category_name' => $catUrl
);

    /* Added $paged variable */

    $exec = new WP_Query($args);

    if($exec->have_posts()):
        echo '<ul class="single-store">';
        while($exec->have_posts()):
            $exec->the_post();
            $categories = get_the_category($post->ID);
                echo "<li class='store-box'>";
                    echo '<a href=" '. get_the_permalink() .' ">';
                        echo '<h1>';
                            echo the_title();
                        echo '</h1>';
                    echo "</a>";

                    echo '<div class="store-info-box">';
                        echo '<span>';
                            echo '<i class="fa fa-map-marker" aria-hidden="true"></i>';
                            echo the_field('localizacao');
                        echo '</span>';
                        echo '<span>';
                            echo $categories[0]->name;
                        echo '</span>';
                    echo '</div>';

                echo "</li>";
        endwhile;
        echo '</ul>';
        if (function_exists(custom_pagination)) {
            custom_pagination($exec->max_num_pages,"",$paged);
        }
    endif;

}
add_action('wp_ajax_catUrl', 'catUrl');
add_action('wp_ajax_nopriv_catUrl', 'catUrl');

JavaScript function JavaScript功能

$('.cat-item').on('click', 'a', function(e){
     e.preventDefault();
     var catUrl = this.href;
     catUrl = catUrl.split('/');

     $.ajax({
         type: "POST",
         url: 'http://localhost/asv/shopping/wp-admin/admin-ajax.php',
         dataType: 'json',
         data: {
              action: catUrl,
              catUrl: catUrl[5]
         },
         success: function(data){
              response(data),
              console.log('test' + data);
         }
     });
});

I've searched this a lot but I didn't find out how to use ajax in wordpress properly. 我已经搜索了很多,但没有找到如何在wordpress中正确使用ajax的方法。

if you are getting results in you console.log('test' + data); 如果您在console.log('test' + data);中获得结果console.log('test' + data); seems like you just need to display this results back in your HTML container by replacing the current content with your ajax response. 似乎您只需要通过用ajax响应替换当前内容,就可以在HTML容器中显示此结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM