0

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!

1 Answer 1

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' );
1
  • This answer is valid but I am searching for something that doesn't need me to add the id every time I create a new sub-page.
    – TPA
    Commented Aug 7, 2019 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.