我正在使用 WordPress 主题,我想从另一个页面的 div 内的页面加载内容。
这是我想要在 div 中加载的页面代码:
page-usercomments.php(这是一个自定义页面模板,我在其中检索当前用户的评论)
<?php
get_header();
?>
<div id="primary" class="row">
<div id="content" class="span9" role="main">
<!-- GET CURRENT USER -->
<?php
global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
?>
<!-- GET COMMENTS OF USER -->
<?php
$args = array(
'user_id' => $current_user->ID, // use user_id
'post_type' => 'debate'
);
$comments = get_comments($args);
?>
<ol class="commentlist">
<?php
wp_list_comments(
array(
'per_page' => 10, //Allow comment pagination
'reverse_top_level' => false //Show the latest comments at the top of the list
),
$comments
);
?>
</ol><!-- .commentlist -->
</div><!-- #content .site-content -->
</div><!-- #primary .content-area -->
<?php get_footer(); // This fxn gets the footer.php file and renders it ?>
我想从另一个页面的 div 中获取此页面的输出,并且我正在考虑使用 AJAX ,这是代码:
更新:
$.ajax({
type: "GET",
url:"http://www.mywebsite.co/cpt/user-comments",
cache: false,
dataType: 'html',
success: function(data){
$("#div").append(data); // <--- look here
},
error: function(){ },
complete: function(){ }
});
});
后来更新:我已经使用 WORDPRESS 解决了这个问题 * AJAX API *
这是正确的方法吗?
而且,我不知道应该如何添加网址,因为为我想要检索的页面创建的永久链接是“mywebsite.com/user-comments” 谢谢!
最佳答案
这是我的 ajax 函数...
您发送 ajaxcall,当它返回成功代码时,获取数据,并将其放入您的 div 中。
$.ajax({
type: "GET",
url: yourUrl,
cache: false,
dataType: 'html',
success: function(data){
$("#div").append(data); // <--- look here
},
error: function(){ },
complete: function(){ }
});
关于javascript - WordPress通过AJAX从页面获取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20354987/