之前@linmumu问起一个query_post用more截断不管用的问题,查找了下原因,记录解决方法,备用。

问题,如在下面的例子,php代码一个用于ajax输出文章的简单过程:

<?php
function ajax_post(){
if( isset($_GET['action'])&& $_GET['action'] == 'ajax_post'){
$ariticle_id=$_GET['id'];
query_posts("p=$ariticle_id");the_post();the_content('继续阅读');
die();
}else{
return;
}
}
add_action('init', 'ajax_post');
?>

the_content('继续阅读');用这个还是只能输出全文,more截断没效果。查阅了官方文档对query_posts的解释,关于使用more截断的问题描述如下 :

If you want to use the Read More functionality with this query, you will need to set the global $more variable to 0.

也就是说如果想要在query_post里面正常使用more截断功能,需要设置全局变量$more为零。还是上面那个例子,对应的改成:

<?php
function ajax_post(){
if( isset($_GET['action'])&& $_GET['action'] == 'ajax_post'){
$ariticle_id=$_GET['id'];
query_posts("p=$ariticle_id");
the_post();
global $more;
$more = 0;
 the_content('继续阅读'); 

die();
}else{
return;
}
}
add_action('init', 'ajax_post');
?>

更新:稍微注意下,“ global $more;$more = 0;”这句应该位于the_post();后面,另外,如果用了query_post的方法,按WordPress官方文档中指出执行query_post之后会影响在其之下的判断函数, 需要在判断函数前加上wp_reset_query重置查询函数。例如if (is_home () ) 这类的判断,不加wp_reset_query就会失效。

  1. heson (2010.10.11 at 13:08) 17楼

    试一试~~~~~~

  2. Solo (2010.10.19 at 13:04) 18楼

    我也看不懂...

  3. 无为 (2010.10.19 at 20:22) 19楼

    嗯,不错!可能以后用得着

  4. inlost (2010.10.24 at 02:21) 20楼

    刚撤出wp的纯粹的路过~

评论分页: 1 2 3

发布评论

邮箱(必填)

换个背景