admin管理员组文章数量:1417691
I am trying to find a way to hide a specific post category from all pages but one (and its child-/ sub-pages).
Let's say I have a page called "Fruits" and it's child-/ sub-pages are "Berries" and "Stone Fruits". Furthermore, I have created a post category "Fruits-Category" and some posts tagged to it. I want these posts just to be shown on the "Fruits", "Berries" and "Stone Fruits" page. Not on the home page, any archives or sidebars or any other pages. When googling, I just find examples for hiding categories on either the home page or the blog page.
Here is my code so far in functions.php
:
function tpa_exclude_technology_cat( $query ) {
if ( !$query->get_queried_object_id('8') ) { // "Fruits"-page has id of 8
$query->set( 'cat', '-3' ); // "Fruits-Category" has id of 3
}
return $query;
}
add_filter( 'pre_get_posts', 'tpa_exclude_technology_cat' );
I am not sure how to get the child-/ sub-pages of "Fruits". Maybe there is a better way to solve this?
Thanks in advance!
I am trying to find a way to hide a specific post category from all pages but one (and its child-/ sub-pages).
Let's say I have a page called "Fruits" and it's child-/ sub-pages are "Berries" and "Stone Fruits". Furthermore, I have created a post category "Fruits-Category" and some posts tagged to it. I want these posts just to be shown on the "Fruits", "Berries" and "Stone Fruits" page. Not on the home page, any archives or sidebars or any other pages. When googling, I just find examples for hiding categories on either the home page or the blog page.
Here is my code so far in functions.php
:
function tpa_exclude_technology_cat( $query ) {
if ( !$query->get_queried_object_id('8') ) { // "Fruits"-page has id of 8
$query->set( 'cat', '-3' ); // "Fruits-Category" has id of 3
}
return $query;
}
add_filter( 'pre_get_posts', 'tpa_exclude_technology_cat' );
I am not sure how to get the child-/ sub-pages of "Fruits". Maybe there is a better way to solve this?
Thanks in advance!
Share Improve this question asked Aug 2, 2019 at 7:32 TPATPA 11 Answer
Reset to default 0try this here i get category on three page
function tpa_exclude_technology_cat( $query ) {
if ( !$query->get_queried_object_id('8') || !$query->get_queried_object_id('Berries page id here') || !$query->get_queried_object_id('Stone Fruits page id here')) { // "Fruits"-page has id of 8
$query->set( 'cat', '-3' ); // "Fruits-Category" has id of 3
}
return $query;
}
add_filter( 'pre_get_posts', 'tpa_exclude_technology_cat' );
本文标签: phpHow to show category just on specific pages
版权声明:本文标题:php - How to show category just on specific pages? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745275582a2651165.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论