doumeng06063991 2015-12-19 13:14
浏览 54

用ajax加载wordpress模板

I want to load my templates with ajax, but I am having struggles (doesn't work at all), mostly with the PHP I guess. I am trying to get whatever template is the correct one using the ID to get the template slug, but doesn't seem to be working.

Here is my code so far:

function theme_js() {

    wp_enqueue_script( 'ajaxstuff', get_template_directory_uri() . '/js/ajax.js', array('jquery'), '', true );
    wp_localize_script( 'ajaxstuff', 'ajaxify', array(
        'ajaxurl' => admin_url( 'admin-ajax.php' )
    ));

}

add_action( 'wp_enqueue_scripts', 'theme_js' );

add_action( 'wp_ajax_nopriv_ajax_submit', 'my_ajax_submit' );
add_action( 'wp_ajax_ajax_submit', 'my_ajax_submit' );

function my_ajax_submit() {

    $pageUrl = $_REQUEST['URL'];
    $postid = url_to_postid( $pageUrl );         
    $post = get_post($postid);
    $pageSlug = get_page_template_slug($postid);

    if ($post) {
    setup_postdata($post);

        get_template_part( $pageSlug );

    }

    exit();
}

And my JS:

    (function($){

   $('body').on('click', 'a', function(e) {

        e.preventDefault();

        var URL = $(this).attr('href');

        animationFunction();

        var ajaxPromise = $.ajax({
            url: ajaxify.ajaxurl,
            type: "post",
            data: {
                action: 'my_ajax_submit',
                url: URL
            },
            dataType: "html"
        });

        var animationPromise = animationFunction();
        $.when(ajaxPromise, animationPromise).done(function(data) {
            $('#page-wrap').html(data[0]);
            TweenMax.to('.page', 0.35, { alpha: 1 });
        });

    });

    function animationFunction() {

      var deferred = $.Deferred();

      TweenMax.to('.page', 0.35, { alpha: 0, onComplete: deferred.resolve  });

      return deferred.promise();

    }

})(jQuery);
  • 写回答

1条回答 默认 最新

  • duanqin2026 2015-12-19 13:42
    关注
    $pageUrl = $_REQUEST['URL'];
    

    $_REQUEST contains an associative array and array indexes are case sensitive, so:

    $pageUrl = $_REQUEST['url'];
    
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图