Very good, There is no way, something is happening to me and I do not control it. I enter the code that you told me in the file functions.php of my child theme and it does not work:
Function my_home_category ($ query) {
If ($ query-> is_home () && $ query-> is_main_query ()) {
$ Query-> set ('cat', '123 ');
}
}
Add_action ('pre_get_posts', 'my_home_category');
Where I put ‘cat’ I replaced it by category and where I put ‘123’ I replaced it with the slug of the category I want it to show and nothing at all, it does not work.
@paconarud16 – if you check the code again you’ll find it is $query, not $ query. You’re also showing an extra space after your category id.
Impossible, uff. I did what you told me, eliminate the spaces and nothing at all. I have a Category that is called ‘bod’ (this is the slug) and I want to not show the posts of this category in the home page post. This is the code that I enter in the function.php according to what you have told me:
Function exclude_category ($query) {
If ($query-> is_home () && $query-> is_main_query ()) {
$Query-> set ('category','-bod');
}
}
Add_action ('pre_get_posts', 'exclude_category');
Nothing at all, the posts are still showing. Crazy.
-
This reply was modified 6 years, 2 months ago by
paconarud16.
1. In PHP variable names are case sensitive. $Query is not the same as $query.
http://php.net/manual/en/language.variables.basics.php
2. The referenced code snippet used the category id. If you want to use the slug you need to use category_name:
https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Wonderful, a thousand thanks, at last, was a syntax error when writing Query. Solved A thousand thanks, a pleasure to count on your support. A greeting.
You’re welcome; glad to help.