douluo3256 2014-09-28 01:33
浏览 48
已采纳

链接到WordPress页面上的特定AJAX数据

On my WordPress website, I'm writing a plugin with a world map that loads a particular post depending on what region is clicked using AJAX. I'd like to include links within the main navigation that point to the map page with a region's post already displayed (ex: the link titled "Middle East" would direct the user to the map page with the Middle East post already displayed). I'm trying to use location.hash but I don't totally understand how it works. Here is my code:

start.js:

jQuery(document).ready(function() {

    // Added code based on jetlej's answer
    jQuery('#vmap').ready( function(event, code, region) {
        $.ajax(get_data.ajaxurl, {
            data: {region: region, 'action': 'get_post_data'},
            dataType: 'json',
                success: function(response) {
                    $("#post").html(response);
                    location.hash = region;
                }
        });
    });

    jQuery('#vmap').vectorMap({
        map: 'world_en',
        backgroundColor: '#fff',
        // more map config options...
        onRegionClick: function(element, code, region)
        {
            if (location.hash) {
                location.hash.replace("#","")+".html"
            }

            $.ajax(get_data.ajaxurl, {
                data: {region: region, 'action': 'get_post_data'},
                dataType: 'json',
                    success: function(response) {
                        $("#post").html(response);
                        location.hash = region;
                }
            });

        }  
    });
});

index.php:

class Post{
    function get_post( $ID ){
        $html = '';
        $post = get_post( $ID );
        if( $post ) {
            $html = $post->post_title;
            $html .= wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
            $html .= $post->post_content;
        }
        return $html;
    }
}

function get_post_data(){

    $post = new Post();
    $result = '';

      // Swtich based on region
      switch($_REQUEST['region']) {
        //Europe
        case 'Russia':
        case 'Albania':
            $result = $post->get_post( 405 );
          break;

        //Africa
        case 'Congo':
        case 'Nigeria':
            $result = $post->get_post( 388 );
          break;

      echo json_encode($result);
      die();
}

add_action('wp_ajax_get_post_data', 'get_post_data');
add_action('wp_ajax_nopriv_get_post_data', 'get_post_data');

And my links look like this:

<a href="http://example.com/where-we-serve/#Russia">Europe</a>

When I click the link, it takes me to the map page but it's in its default state with no posts displayed. I'm getting the hash values in the URL when I click the map, but they don't seem to actually have any effect on the state of the page. Is this an issue with how I'm using location.hash, an issue with the link, or is this just the wrong approach?

  • 写回答

1条回答 默认 最新

  • doukui2011 2014-09-28 06:39
    关注

    You don't seem to be checking for the hash value on each page load. You're only checking if the map is clicked.

    For getting the map to show the region from the hash, you just need to use your map plugin's built-in 'selectedRegion' parameter.

    Eg.

        jQuery(document).ready(function() {
    
            region = null;
            // If there is a hash, then grab it and update the page to reflect that region
            if(location.hash){
    
                // Remove the '#' from the hash
                region = location.hash.split('#')[1];
    
                // Update #post with the region post info
                $.ajax(get_data.ajaxurl, {
                    data: {region: region, 'action': 'get_post_data'},
                    dataType: 'json',
                    success: function(response) {
                        $("#post").html(response);
                    }
                });    
    
            }
    
            jQuery('#vmap').vectorMap({
                map: 'world_en',
                backgroundColor: '#fff',
                // Set the pre-selected to region to that hash, or null if there is no hash
                selectedRegion: region,
                onRegionClick: function(element, code, region){
                    if (location.hash) {
                        location.hash.replace("#","")+".html"
                    }
    
                    $.ajax(get_data.ajaxurl, {
                        data: {region: region, 'action': 'get_post_data'},
                        dataType: 'json',
                        success: function(response) {
                            $("#post").html(response);
                            location.hash = region;
                        }
                    });
                }  
            });
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line