在WordPress博客首页排除某些特定分类

前端之家收集整理的这篇文章主要介绍了在WordPress博客首页排除某些特定分类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

对于一般的wordpress博客主题来说,首页大都是调用最新的文章列表,如果我们希望在首页排除某些特定的分类,该如何操作呢?最简单的方法就是通过 pre_get_posts 钩子来改变主查询

下面我们就向大家介绍一下实现的方法,切换到主题目录,打开 functions.PHP 文件,加入以下代码

Crayon-5c891cba700df402491394" 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-5c891cba700df402491394-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cba700df402491394-2">2
Crayon-num" data-line="Crayon-5c891cba700df402491394-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cba700df402491394-4">4
Crayon-num" data-line="Crayon-5c891cba700df402491394-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cba700df402491394-6">6
Crayon-num" data-line="Crayon-5c891cba700df402491394-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cba700df402491394-8">8
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-5c891cba700df402491394-1">Crayon-t">functionCrayon-h"> Crayon-e">exclude_category_homeCrayon-sy">(Crayon-h"> Crayon-v">$queryCrayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891cba700df402491394-2">Crayon-h">Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-v">$queryCrayon-i">is_homeCrayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891cba700df402491394-3">Crayon-h">Crayon-v">$queryCrayon-e">setCrayon-sy">(Crayon-h"> Crayon-s">'cat'Crayon-sy">,Crayon-h"> Crayon-s">'-10,-11'Crayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cba700df402491394-4">Crayon-h">Crayon-sy">}
Crayon-line" id="Crayon-5c891cba700df402491394-5">Crayon-h">Crayon-k ">returnCrayon-h"> Crayon-v">$queryCrayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cba700df402491394-6">Crayon-sy">}
Crayon-line" id="Crayon-5c891cba700df402491394-7">Crayon-h">
Crayon-line Crayon-striped-line" id="Crayon-5c891cba700df402491394-8">Crayon-e">add_filterCrayon-sy">(Crayon-h"> Crayon-s">'pre_get_posts'Crayon-sy">,Crayon-h"> Crayon-s">'exclude_category_home'Crayon-h"> Crayon-sy">)Crayon-sy">;

根据您自己的需要,修改代码的第 3 行中的分类ID,比如你想排除分类 2 和 3,就将第 3 行改为:

Crayon-5c891cba700e6264208852" 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-5c891cba700e6264208852-1">1
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-5c891cba700e6264208852-1">Crayon-v">$queryCrayon-e">setCrayon-sy">(Crayon-h"> Crayon-s">'cat'Crayon-sy">,Crayon-h"> Crayon-s">'-2,-3'Crayon-h"> Crayon-sy">)Crayon-sy">;

如果你不希望在顶部显示置顶文章,可以在第 3 行下面添加

Crayon-5c891cba700e8627290721" 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-5c891cba700e8627290721-1">1
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-5c891cba700e8627290721-1">Crayon-v">$queryCrayon-e">setCrayon-sy">(Crayon-h"> Crayon-s">'ignore_sticky_posts'Crayon-sy">,Crayon-h"> Crayon-s">'1'Crayon-h"> Crayon-sy">)Crayon-sy">;

如果出现新文章出现在最下面,老文章却在上面(也就是文章排序倒过来了),那添加如下代码试试:

Crayon-5c891cba700ea778191910" 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-5c891cba700ea778191910-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cba700ea778191910-2">2
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-5c891cba700ea778191910-1">Crayon-v">$queryCrayon-e">setCrayon-sy">(Crayon-h"> Crayon-s">'orderby'Crayon-sy">,Crayon-h"> Crayon-s">'date'Crayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cba700ea778191910-2">Crayon-v">$queryCrayon-e">setCrayon-sy">(Crayon-h"> Crayon-s">'order'Crayon-sy">,Crayon-h"> Crayon-s">'DESC'Crayon-h"> Crayon-sy">)Crayon-sy">;

原文链接:https://www.f2er.com/wordpress/238363.html

猜你在找的wordpress相关文章