javascript - 使用ajax重新加载wordpress php部分

标签 javascript php jquery ajax wordpress

我使用的 WordPress 主题有不同的部分,例如 footer.php 或 header.php。我想要做的是使用 jquery 重新加载整个页面的一部分,这样整个页面就不会刷新。下面是我的代码,其中包含我想要重新加载的 php 部分和我使用的 jquery。

php 代码块内的 Jquery 代码

<script type="text/javascript">
                jQuery(document).ready(function(){
                    var thedata = '.json_encode($this->query_args).';
                    jQuery( ".facetwp-page" ).click(function() {


                        jQuery.ajax({
                            type: "post",
                            url:  "'.admin_url("admin-ajax.php").'",
                            data: { "action" : "query_for_map", "mydata" : thedata },
                            success: function(response, data){
                                console.log(response);
                                console.log(data);
                                jQuery("#map-head").load("'.get_template_directory_uri().'/banners/map_based_banner.php");
                                google.maps.event.trigger(map, "resize");

                            }
                        });
                    });
                });

                </script>

要重新加载的 php 模板部分

<?php
session_start();
include('/home/javy1103/public_html/wp-blog-header.php');
global $paged;
$properties_for_map = array(
                        'post_type' => 'property',
                        'posts_per_page' => 12,
                        'paged' => $paged
                    );

if( is_page_template('template-search.php') || is_page_template('template-search-sidebar.php') ){

    /* Apply Search Filter */
    $properties_for_map = apply_filters( 'real_homes_search_parameters', $properties_for_map );

}elseif(is_page_template('template-home.php')){

    /* Apply Homepage Properties Filter */
    $properties_for_map = apply_filters( 'real_homes_homepage_properties', $properties_for_map );

}elseif(is_tax()){

    global $wp_query;
    /* Taxonomy Query */
    $properties_for_map['tax_query'] = array(
                                            array(
                                                'taxonomy' => $wp_query->query_vars['taxonomy'],
                                                'field' => 'slug',
                                                'terms' => $wp_query->query_vars['term']
                                            )
                                        );

}

function query_for_map() {

return json_decode($_POST['mydata']);

}
add_action('wp_ajax_query_for_map', 'query_for_map');
add_action('wp_ajax_nopriv_query_for_map', 'query_for_map');

$mapdata = $_SESSION['queryMap'];

$properties_for_map_query = new WP_Query( $mapdata );


$properties_data = array();

if ( $properties_for_map_query->have_posts() ) :

    while ( $properties_for_map_query->have_posts() ) :

        $properties_for_map_query->the_post();

        $current_prop_array = array();

        /* Property Title */
        $current_prop_array['title'] = get_the_title();

        /* Property Price */
        $current_prop_array['price'] = get_property_price();

        /* Property Location */
        $property_location = get_post_meta($post->ID,'REAL_HOMES_property_location',true);
        if(!empty($property_location)){
            $lat_lng = explode(',',$property_location);
            $current_prop_array['lat'] = $lat_lng[0];
            $current_prop_array['lng'] = $lat_lng[1];
        }

        /* Property Thumbnail */
        if(has_post_thumbnail()){
            $image_id = get_post_thumbnail_id();
            $image_attributes = wp_get_attachment_image_src( $image_id, 'property-thumb-image' );
            if(!empty($image_attributes[0])){
                $current_prop_array['thumb'] = $image_attributes[0];
            }
        }

        /* Property Title */
        $current_prop_array['url'] = get_permalink();

        /* Property Map Icon Based on Property Type */
        $property_type_slug = 'single-family-home'; // Default Icon Slug

        $type_terms = get_the_terms( $post->ID,"property-type" );
        if(!empty($type_terms)){
            foreach($type_terms as $typ_trm){
                $property_type_slug = $typ_trm->slug;
                break;
            }
        }

        if(file_exists(get_template_directory().'/images/map/'.$property_type_slug.'-map-icon.png')){
            $current_prop_array['icon'] = get_template_directory_uri().'/images/map/'.$property_type_slug.'-map-icon.png';
        }else{
            $current_prop_array['icon'] = get_template_directory_uri().'/images/map/single-family-home-map-icon.png';
        }

        $properties_data[] = $current_prop_array;
    endwhile;
    wp_reset_query();

?>

最佳答案

您可以通过Ajax生成所有服务器端(PHP)工作,而无需刷新页面,然后您可以在HTML中使用它,您不能替换页面的PHP。 例如:如果您想从数据库中获取一些数据,那么您可以向 PHP 文件发送 ajax 请求,然后获取数据并获取结果,然后以 HTML 形式显示。

关于javascript - 使用ajax重新加载wordpress php部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352761/

相关文章:

如果在获取期间数据为 Null 或为空,PHP Mysqli 将替换逗号

jquery - Superfish 菜单不淡入

javascript - 如何在 Bootstrap 选项卡中添加关闭图标?

将 JavaScript 对象插入数组

javascript - 了解 React 基本示例

javascript - Angularjs - 如何单元测试表单验证

php - MYSQL error and not null 仍然向mysql添加数据

javascript - TypeScript 中的扩展如何工作?

php - Symfony3 尝试使用表单和 Twig 保存到数据库中

jquery - 导航到特定页面时如何保持事件选项卡