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 1
Add a comment  | 

1 Answer 1

Reset to default 0

try 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