admin管理员组

文章数量:1022956

We have a lot of blog post categories, and posts belonging to all but one category should show on the blog page. The other category should show on the Case Studies page.

The solutions I've found make a global exclusion using functions.php, which would break the Case Studies sort.

I'm comfortable working with PHP but not sure how I would go about excluding the category on just one page.

Edit: Here is an example of category exclusion that I think would globally exclude category with ID of 1 in functions.php:

function exclude_category($query) {
    $query->set('cat','-1');
    return $query;
}
add_filter('pre_get_posts','exclude_category');

Below is one that is specific to a feed page. Does it know to exclude it on the feed page because feed is the slug of the page to exclude for?

function exclude_category($query) {
    if ($query->is_feed) {
        $query->set('cat','-1');
    }
    return $query;
}
add_filter('pre_get_posts','exclude_category');

We have a lot of blog post categories, and posts belonging to all but one category should show on the blog page. The other category should show on the Case Studies page.

The solutions I've found make a global exclusion using functions.php, which would break the Case Studies sort.

I'm comfortable working with PHP but not sure how I would go about excluding the category on just one page.

Edit: Here is an example of category exclusion that I think would globally exclude category with ID of 1 in functions.php:

function exclude_category($query) {
    $query->set('cat','-1');
    return $query;
}
add_filter('pre_get_posts','exclude_category');

Below is one that is specific to a feed page. Does it know to exclude it on the feed page because feed is the slug of the page to exclude for?

function exclude_category($query) {
    if ($query->is_feed) {
        $query->set('cat','-1');
    }
    return $query;
}
add_filter('pre_get_posts','exclude_category');
Share Improve this question edited Apr 4, 2019 at 19:37 NAMS asked Apr 3, 2019 at 20:14 NAMSNAMS 1013 bronze badges 3
  • 1 Hello & welcome :) May you edit your question and post the code you used for the global exclusion, please? I think it will help you find answers faster when people see what you have tried already and why it is not working. Also, do you have a specific template in place for the Case Studies category (for example, category-case-studies.php) or are you using the default category.php or archive.php or falling back on index.php? – jsmod Commented Apr 3, 2019 at 20:26
  • Yes please add the code you are looking at to exclude the one category. It likely needs a condition applied to limit the impact. The preferred way to approach this is filtering the main query with a function hooked to pre_get_posts. – jdm2112 Commented Apr 3, 2019 at 23:58
  • Thank you both. I edited the question with the code i found. I do not have a specific template, I actually have not used or overridden custom templates but would like to learn how. I see I can select a template to use in "Page Attributes" but do not know where to create a new template file to be selected there. – NAMS Commented Apr 4, 2019 at 19:35
Add a comment  | 

1 Answer 1

Reset to default 0

I resolved this by creating a custom page template for my Blog page to exclude posts of the category Case Studies.

I followed this tutorial more or less and was successful adding the category exclusion code in my original post to exclude the case studies.

We have a lot of blog post categories, and posts belonging to all but one category should show on the blog page. The other category should show on the Case Studies page.

The solutions I've found make a global exclusion using functions.php, which would break the Case Studies sort.

I'm comfortable working with PHP but not sure how I would go about excluding the category on just one page.

Edit: Here is an example of category exclusion that I think would globally exclude category with ID of 1 in functions.php:

function exclude_category($query) {
    $query->set('cat','-1');
    return $query;
}
add_filter('pre_get_posts','exclude_category');

Below is one that is specific to a feed page. Does it know to exclude it on the feed page because feed is the slug of the page to exclude for?

function exclude_category($query) {
    if ($query->is_feed) {
        $query->set('cat','-1');
    }
    return $query;
}
add_filter('pre_get_posts','exclude_category');

We have a lot of blog post categories, and posts belonging to all but one category should show on the blog page. The other category should show on the Case Studies page.

The solutions I've found make a global exclusion using functions.php, which would break the Case Studies sort.

I'm comfortable working with PHP but not sure how I would go about excluding the category on just one page.

Edit: Here is an example of category exclusion that I think would globally exclude category with ID of 1 in functions.php:

function exclude_category($query) {
    $query->set('cat','-1');
    return $query;
}
add_filter('pre_get_posts','exclude_category');

Below is one that is specific to a feed page. Does it know to exclude it on the feed page because feed is the slug of the page to exclude for?

function exclude_category($query) {
    if ($query->is_feed) {
        $query->set('cat','-1');
    }
    return $query;
}
add_filter('pre_get_posts','exclude_category');
Share Improve this question edited Apr 4, 2019 at 19:37 NAMS asked Apr 3, 2019 at 20:14 NAMSNAMS 1013 bronze badges 3
  • 1 Hello & welcome :) May you edit your question and post the code you used for the global exclusion, please? I think it will help you find answers faster when people see what you have tried already and why it is not working. Also, do you have a specific template in place for the Case Studies category (for example, category-case-studies.php) or are you using the default category.php or archive.php or falling back on index.php? – jsmod Commented Apr 3, 2019 at 20:26
  • Yes please add the code you are looking at to exclude the one category. It likely needs a condition applied to limit the impact. The preferred way to approach this is filtering the main query with a function hooked to pre_get_posts. – jdm2112 Commented Apr 3, 2019 at 23:58
  • Thank you both. I edited the question with the code i found. I do not have a specific template, I actually have not used or overridden custom templates but would like to learn how. I see I can select a template to use in "Page Attributes" but do not know where to create a new template file to be selected there. – NAMS Commented Apr 4, 2019 at 19:35
Add a comment  | 

1 Answer 1

Reset to default 0

I resolved this by creating a custom page template for my Blog page to exclude posts of the category Case Studies.

I followed this tutorial more or less and was successful adding the category exclusion code in my original post to exclude the case studies.

本文标签: categoriesExclude posts in a category on one page but show those posts on a different page