DragonWar% 2016-06-10 14:18 采纳率: 0%
浏览 28

WP Ajax编码不起作用

Ok so I am having trouble with converting my html design website to wp. I have everything working except for the ajax code that I am using to load the content into a specified div without reloading the page. This way my music player plays continuously without restarting the music player with every page load or link click. The code I was using for the html of my website worked perfectly exactly how i wanted it to. Please take a look at the page http://www.trillumonopoly.com/index2.html

but when I came here for help I was suggested to used some changes to make it work with wordpress.... I find that the content still doesnt load without reloading the entire page and then the music player starts from the beginning all over again. From a listener and consumer stand point this is annoying and I dont want to lose clients, or fans becuase of this issue so I have been waiting to launch my website untill this problem is fixed so maybe I can have someone assist me with this. please

Here is the Code I was told to put in my functions php

 /**
   * Init js script and its variables
   */
  add_action( 'wp_enqueue_scripts', 'init_js_vars' );
  function init_js_vars() {
    wp_enqueue_script( 'menu_nav', get_template_directory_uri().'/js/nav.js', array( 'jquery') );


    wp_localize_script( 'menu_nav', 'theme', array(
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
            'nonce' => wp_create_nonce( 'ajax-get-page-content' ),
            'page' => 4
        ) );
    }

    /**
     * Handle ajax request, returns content of specific page
     */
    add_action( 'wp_ajax_get_page_content', 'get_page_content' );
    add_action( 'wp_ajax_nopriv_get_page_content', 'get_page_content' );
    function get_page_content() {
        check_ajax_referer( 'ajax-get-page-content', 'nonce' );

        $post_id = absint( $_POST['page_id'] );

        $post = get_post( $post_id );
        $content = apply_filters('the_content', $post->post_content); 

        wp_send_json_success( array( 'content' => $content ) );
    }

heres the JS/ajax code that I am using (Which still doesnt function correctly as in the link i provided)

$( document ).ready(function() {
  $.ajax({
    url: theme.ajaxurl,
    method: 'GET',
        data: {
        action: 'get_page_content',
        nonce: theme.nonce,
        page_id: 
    }
    success: function( response ) {
        $('#contentarea').html( response.content );
    }
  });
});

See the script live in action here http://wp.trillumonopoly.com Please help me to figure this out to make it function like it does in my html version of the website from the first link I provided above. (few pages are missing but you can still get the just of what I am trying to do)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么