wordpress首页使用ajax实现翻页方法。那么我们为什么要对wordpress博客的首页进行ajax分页,有人说wordpress首页采用ajax分页可以减轻服务器数据提交的数量,使用ajax实现翻页只会刷新用户想要的内容区体验度也就能得到很大程度的提升。特别是对于大多数图片较多的网站如果一次刷新翻页时不仅加载图片区同时也加载了整站这样没有必要,加载速度较慢的Blog,AJAX是个非常不错的选择。
wordpress首页ajax分页实现方法如下:
1、首先当然是加载jquery库,如果您的主题已经加载了jquery库,则本步骤可以略去,jquery版本保持在1.2.3以上。
[code]<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js”></script>[/code]
2、将下面那段JS保存为index_ajax_navi.js,并在header部分中引用加载,但是要在jquery库之后加载:
[code]var navi=’.pagenavi’;//变量声明 .pagenavi换成你主题中分页的CSS类名
var navi_a=’.pagenavi a’;//变量声明 .pagenavi a换成你主题中分页a标签的CSS类名
var content=’#content’;//变量声明 #content换成你主题中用于AJAX刷新部分的ID名
$(document).ready(function index_ajax_navi (){
$(navi_a).click(function() {
var z = $(this).attr(“href”);
$.ajax({
url: z,
type:”POST”,
data:”action=index_ajax_navi”,
beforeSend:function()
{
document.body.style.cursor = ‘wait’;
var C=1.0;
$(content).css({opacity:C,MozOpacity:C,KhtmlOpacity:C,filter:’alpha(opacity=’ + C * 100 + ‘)’});
$(navi).html(‘<img src=”loading图片的路径”><a>正在加载中…</a>’);
},
error: function(request)
{
alert(request.responseText);
},
success: function (data)
{
$(content).html(data);
document.body.style.cursor = ‘auto’;
var C=1; //设置翻页的时候,AJAX部分的透明度,可以为0.5;0.6等
$(content).css({opacity:C,MozOpacity:C,KhtmlOpacity:C,filter:’alpha(opacity=’ + C * 100 + ‘)’});
//$body.animate({ scrollTop: ‘0px’}, 1000);
index_ajax_navi();//翻页后DOM发生变化,重新绑定该函数
//$body.animate( { scrollTop: $(content).offset().top – 150}, 1000);
//$body.animate({ scrollTop: ‘0px’}, 1000);
jQuery(‘html, body’).animate({scrollTop:$(content).offset().top – 100}, ‘slow’);
}
});
return false;
});
})[/code]
上面代码中的相关ID,需要根据你自己主题修改一下
3、将如下代码写到你的后台functions.php中
[code]function index_ajax_navi(){
if( isset($_POST[‘action’])&& $_POST[‘action’] == ‘index_ajax_navi’){
include_once TEMPLATEPATH.’/index_list.php’; //注意修改为你自己的文件的位置
die();
}else{
return;
}
}
add_action(‘template_redirect’, ‘index_ajax_navi’);[/code]
4、把你主题的index.php写成两个文件,一个是框架文件,一个是index_list.php。是ajax请求需要重新加载的页面。这里要注意,并不是要写成这个样子,这是一种思想。就是index_list.php是需要被重新加载的,每个人把自己的index.php需要在点击页面之后重新加载的东西提出来,写到index_list.php之中即可。
index.php写成类似下面这样(每个人的主题都不同,请自己修改):
[code]<?php get_header(); ?>
<?php
$posts = query_posts($query_string . ‘&orderby=date&showposts=8’);
pre_next();
?>
<div id=”content”>
<?php include_once TEMPLATEPATH.’/index_list.php’; ?>
</div> <!–end: content–>
<?php wp_reset_query(); ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>[/code]
index_list.php写成这个样子:
[code]<?php while (have_posts()) { the_post(); ?>
<div class=”post” id=”post-<?php the_ID(); ?>”>
<div class=”title”><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”打开 <?php the_title_attribute(); ?> 吧,还等什么?”><h2><?php the_title(); ?></h2></a></div>
<?php include (TEMPLATEPATH . ‘/myad/adindex.php’); ?>
<div class=”headline”>
<?php if(function_exists(‘wp_thumbnails_for_homepage’)) { wp_thumbnails_for_homepage(); } ?>
<span><?php the_time(“M<b\i\g>j</b\i\g>”) ?></span>
</div>
<div class=”text” style=”margin: 0;”>
<?php the_content(‘<br />continue reading »’); ?></div>
<div class=”postother” style=”background:#E7E7DA;”>
<span class=”category”><?php the_category(‘, ‘) ?> </span>
<span class=”comments”><?php comments_popup_link(‘添加评论’, ‘1条评论’, ‘%条评论’); ?> </span>
<span class=”hits”><?php if(function_exists(‘the_views’)) { the_views(); } ?> </span>
</div>
</div>
<?php } ?>
<div class=”pageNavi”>
<?php if (function_exists(‘pagenavi’)) { pagenavi(); } ?>
</div>[/code]
ok,现在这样就可以实现首页的ajax翻页效果了。不过每个人的主题设置不同,上面部分步骤中有些地方需要根据自己的主题设置自己的参数。特别是包含分页链接的div的class。我这里是.pagenavi。还有就是包含index_list.php的 div id=”content”,需要在js中填入这两个关键的div