2024年10月12日 建站教程
wordpress模板在“设置”->“阅读设置”->“博客页面至多显示”可以全局设置所有列表,tag标签页及首页的文章条数!但是如果需要单独设置的话需要做一些调整!
1、自定义首页、分类、tag标签页文章数(PC端/移动端不同)
<?php
if ( have_posts() ) {
if(strpos($_SERVER['HTTP_HOST'], 'm.**.cn') !== false){
//移动端显示30条
query_posts($query_string . '&showposts=30');
} else {
//pc端显示默认
query_posts($query_string);
}
// Load posts loop.
while ( have_posts() ) {
the_post();
if(strpos($_SERVER['HTTP_HOST'], 'm.**.cn') !== false){
//移动端显示模板
get_template_part( 'template-parts/content/content-m' );
}else{
//pc端实现模版
get_template_part( 'template-parts/content/content' );
}
}
}
?>
2、自定义首页、分类文章数
add_action( ‘pre_get_posts’, ‘zm_set_posts_per_page’ );
function zm_set_posts_per_page( $query ) {
if ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_query’] ) && ( $query->is_search() ) ) {
$query->set( ‘posts_per_page’, 5 );
}
elseif ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_the_query’] ) && ( $query->is_archive() ) ) {
$query->set( ‘posts_per_page’, 10 );
}
return $query;
}
3、自定义不同分类显示不同的文章数
add_action( ‘pre_get_posts’, ‘zm_set_posts_per_page’ );
function zm_set_posts_per_page( $query ) {
if ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_the_query’] ) && ( is_category(array(1,2)) ) ) {
$query->set( ‘posts_per_page’, 5 );
}
elseif ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_the_query’] ) && ( is_category(array(3,4)) ) ) {
$query->set( ‘posts_per_page’, 8 );
}
elseif ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_the_query’] ) && ( is_category(array(5,6)) ) ) {
$query->set( ‘posts_per_page’, 10 );
}
}
本文链接:http://so.lmcjl.com/news/15202/