微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WordPress中自定义不同归档页面每页显示条数

wordpress后台设置中,我们可以通过 阅读 > 博客页面至多显示 设置每页最多显示文章数目,但此设置只能将所有的类别(首页分类目录页、标签页、作者页显示文章数都设置成一个固定值。

使用wordpress建站的过程中,如果我们想给不同归档页面首页分类目录页、标签页、作者页自定义每页显示条数。比如首页每页最多显示10篇文章分类页每页最多显示20篇文章标签页每页最多显示30篇文章。这篇wordpress教程里,我们就向大家介绍一下具体的实现方法

我们先来了解一下wordpress内部提供的函数来判断某个页面的类别:

Crayon-5c891ce15dce2788634804" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891ce15dce2788634804-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce15dce2788634804-2">2
Crayon-num" data-line="Crayon-5c891ce15dce2788634804-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce15dce2788634804-4">4
Crayon-num" data-line="Crayon-5c891ce15dce2788634804-5">5
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891ce15dce2788634804-1">Crayon-e">is_homeCrayon-sy">(Crayon-sy">)
Crayon-line Crayon-striped-line" id="Crayon-5c891ce15dce2788634804-2">Crayon-e">is_pagedCrayon-sy">(Crayon-sy">)
Crayon-line" id="Crayon-5c891ce15dce2788634804-3">Crayon-e">is_singleCrayon-sy">(Crayon-sy">)
Crayon-line Crayon-striped-line" id="Crayon-5c891ce15dce2788634804-4">Crayon-e">is_categoryCrayon-sy">(Crayon-sy">)
Crayon-line" id="Crayon-5c891ce15dce2788634804-5">Crayon-e">is_tagCrayon-sy">(Crayon-sy">)

上面的函数依次判断是否是首页、是否是分页显示、是否是文章页、是否是分类目录页、是否是标签页。wordpress中提供了各种钩子 (Hooks ),通过这些钩子我们可以修改wordpress中的一些内部行为。在查询的时候可以通过 posts_per_page 设置每页显示文章数,如下:

Crayon-5c891ce15dcea198573518" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891ce15dcea198573518-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce15dcea198573518-2">2
Crayon-num" data-line="Crayon-5c891ce15dcea198573518-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce15dcea198573518-4">4
Crayon-num" data-line="Crayon-5c891ce15dcea198573518-5">5
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891ce15dcea198573518-1">Crayon-sy">$Crayon-v">argsCrayon-h"> Crayon-o">=Crayon-h"> Crayon-t">arrayCrayon-sy">(
Crayon-line Crayon-striped-line" id="Crayon-5c891ce15dcea198573518-2">Crayon-h">Crayon-s">'caller_get_posts'Crayon-h"> Crayon-o">=Crayon-h"> Crayon-cn">1
Crayon-line" id="Crayon-5c891ce15dcea198573518-3">Crayon-h">Crayon-s">'posts_per_page'Crayon-h"> Crayon-o">=Crayon-h"> Crayon-cn">10
Crayon-line Crayon-striped-line" id="Crayon-5c891ce15dcea198573518-4">Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891ce15dcea198573518-5">Crayon-e">query_postsCrayon-sy">(Crayon-sy">$Crayon-v">argsCrayon-sy">)Crayon-sy">;

上面的代码片段表示我们查询的时候需要每页最多显示10篇文章。我们可以通过重写 pre_get_posts 钩子来自定义不同归档页面每页显示文章数量。如果我们想在首页每页最多显示10篇文章分类页每页最多显示20篇文章标签页每页最多显示30篇文章。切换到主题目录,打开functions.PHP文件,加入以下代码

Crayon-5c891ce15dcec944188119" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891ce15dcec944188119-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce15dcec944188119-2">2
Crayon-num" data-line="Crayon-5c891ce15dcec944188119-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce15dcec944188119-4">4
Crayon-num" data-line="Crayon-5c891ce15dcec944188119-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce15dcec944188119-6">6
Crayon-num" data-line="Crayon-5c891ce15dcec944188119-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce15dcec944188119-8">8
Crayon-num" data-line="Crayon-5c891ce15dcec944188119-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce15dcec944188119-10">10
Crayon-num" data-line="Crayon-5c891ce15dcec944188119-11">11
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce15dcec944188119-12">12
Crayon-num" data-line="Crayon-5c891ce15dcec944188119-13">13
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891ce15dcec944188119-1">Crayon-t">functionCrayon-h"> Crayon-e">wp_posts_per_pageCrayon-sy">(Crayon-v">$queryCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891ce15dcec944188119-2">Crayon-h">Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-e">is_homeCrayon-sy">(Crayon-sy">)Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891ce15dcec944188119-3">Crayon-h">Crayon-v">$queryCrayon-e">setCrayon-sy">(Crayon-s">'posts_per_page'Crayon-sy">,Crayon-h"> Crayon-cn">10Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891ce15dcec944188119-4">Crayon-h">Crayon-sy">}
Crayon-line" id="Crayon-5c891ce15dcec944188119-5">Crayon-h">Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-e">is_categoryCrayon-sy">(Crayon-sy">)Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891ce15dcec944188119-6">Crayon-h">Crayon-v">$queryCrayon-e">setCrayon-sy">(Crayon-s">'posts_per_page'Crayon-sy">,Crayon-h"> Crayon-cn">20Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891ce15dcec944188119-7">Crayon-h">Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891ce15dcec944188119-8">Crayon-h">Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-e">is_tagCrayon-sy">(Crayon-sy">)Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891ce15dcec944188119-9">Crayon-h">Crayon-v">$queryCrayon-e">setCrayon-sy">(Crayon-s">'posts_per_page'Crayon-sy">,Crayon-h"> Crayon-cn">30Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891ce15dcec944188119-10">Crayon-h">Crayon-sy">}Crayon-h"> Crayon-c">//endif
Crayon-line" id="Crayon-5c891ce15dcec944188119-11">Crayon-sy">}Crayon-h"> Crayon-c">//function
Crayon-line Crayon-striped-line" id="Crayon-5c891ce15dcec944188119-12">Crayon-h">
Crayon-line" id="Crayon-5c891ce15dcec944188119-13">Crayon-e">add_actionCrayon-sy">(Crayon-s">'pre_get_posts'Crayon-sy">,Crayon-h"> Crayon-s">'wp_posts_per_page'Crayon-sy">)Crayon-sy">;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐