wordpress自定义设置首页、分类、tag标签页文章数

500 ℃

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 );
  }
}  

wordpress如何从指定数据库读取文章静态生成

WordPress教程:根据用户登录状态动态跳转不同页面

wordpress想让某个链接不进行跳转代码示例

WordPress的SaaS属性有什么优点

WordPress故障排除大全,网站访问不了解决方法

标签: wordpress优化, wordpress入门, wordpress判断

上面是“wordpress自定义设置首页、分类、tag标签页文章数”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。

当前网址:https://m.ipkd.cn/webs_13762.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

百度echart设置loading加载效果
在vuejs中引入滴滴移动端组件(Mand Mobile)
js如何禁用浏览器右键功能
织梦为dede:tag标签增加[field:global.autoindex/]标签
JavaScript开发小技巧之各种时间操作