首页文章载入Ajax化
不知道是什么原因,本站有些文章在搜索引擎中的位置反而在首页的后面。比如有某篇文章中提到了”xxxx”,文章发布后会在首页显示,当搜素引擎抓取后搜索本站中的”xxxx”时,首页反而排在了该文章的前面。浏览者点击搜索结果进入本站时,如果是首页,很可能文章已经不在首页了。为了解决这个问题想让首页中显示的文章内容全部由ajax载入。这样就避免了搜索引擎对首页显示的文章内容进行抓取,也加快了首页的载入速度。
一个自己写的小插件,主要功能是返回所请求文章的摘要,实现首页文章载入的AJAX化,JS部分非原创,修改而已(下载地址在文章末尾)。
<?php /* Plugin Name: myAjax Plugin URI: Description: This only a test! Author: opsunv Version: 0.0.1 Author URI: http://www.opsunv.com */ $myAjax_dir = get_bloginfo('wpurl').'/wp-content/plugins/myAjax'; function load_post_for_index($id){ $post=new WP_Query(); $post->query('p='.$id); if ($post->have_posts()){ $post->the_post(); return the_content('Read more...'); }else { return "Sorry no archives match!"; } } function myAjax(){ $myAction=$_GET['do']; $id=$_GET['id']; if($myAction){ if($myAction=='load_post'&&$id){ echo load_post_for_index($id); exit; } } } function echoJs(){ global $myAjax_dir; if(is_home()){ echo '<script type="text/javascript" src="'.$myAjax_dir.'/post.js"></script>'; } } add_action('wp_footer','echoJs'); add_action('parse_request','myAjax'); ?>
写本插件时遇到的一个问题:如果使用query_posts() 来获取得到的文章内容似乎不能解析wordpress的more标签,所以另外实例化了一个WP_Query。
使用方法:
1.把首页中的输出文章内容的代码去掉,如主题index.php中类似于
<?php the_content()?>
2.确保你的网页中添加了jquery。
3.下载插件,激活。Download
我再顶
送IP的来了…
鄙视此类行为~
iNove主题删掉后首页只剩标题了
可以问问你的主题是怎么修改的么?