WordPress指定文章显示数量代码<?php query-posts('orderby="">?php>
添加下面代码到主题function.php文件里,下面有具体说明
function custom_posts_per_page($query){
if(is_home()){
$query->set('posts_per_page',8);//首页每页显示8篇文章
}
if(is_search()){
$query->set('posts_per_page',-1);//搜索页显示所有匹配的文章,不分页
}
if(is_archive()){
$query->set('posts_per_page',25);//archive每页显示25篇文章
}//endif
}//function
//this adds the function above to the 'pre_get_posts' action
add_action('pre_get_posts','custom_posts_per_page');
说明:
用你问题上说的query_posts有以下缺点:
第一,增加查询次数
第二,灵活度不高,如果分类、标签有自己的模板,还需要到那些模板里重复query_posts的把戏。
第三,query_posts使用时需特别小心,如果忘记恢复全局变量,可能会出现莫名其妙的错误。
PS:上面的解答来自solagirl的一文 “WordPress根据页面类型指定每页显示的文章数”
wordpress怎样给文章添加描述
你所说的描述是摘要吧?
是的话,文章编辑时候,在右上角有个“显示选项”按钮,打开后会看到有个可以勾选“摘要”的选框,勾选后,在文章编辑框下出现一个框,用来填写摘要的。
填写过后,怎么在前台显示呢?
使用以下代码调用显示
$description = get_the_excerpt();
}else {
$description = mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,220,"。");
$description = str_replace("\n","",$description);
$description = empty($description)? $keywords : $description;
}
?>;当有填写摘要时候,显示你填写的摘要内容;
如果没填写,则显示文章的内容(220字数),\n这句是去换行符;
如果文章没文字等内容,则以标签(关键词)作为摘要内容。
关键词(或标签)的代码我没写。
转载请注明出处51数据库 » wordpress文章总数
殷娘娘