php - How to show category just on specific pages?

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

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' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745275583a4620020.html

相关推荐

  • php - How to show category just on specific pages?

    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

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信