- This topic has 19 replies, 3 voices, and was last updated 5 years, 7 months ago by
Leo.
-
AuthorPosts
-
October 18, 2018 at 9:57 am #704343
Francisco
Hi.
I am using GP Premium 1.7.2.
I have 10 different post categories, but I want to publish only 8 of them in the blog page. The other 2 I want to publish in a widget. How can I control the categories I want to display in the Blog page?
October 18, 2018 at 10:59 am #704405Leo
StaffCustomer SupportHi there,
This should help: https://generatepress.com/forums/topic/exclude-category-from-loop/#post-111543
November 12, 2018 at 3:08 am #725015MÃ¥rten
Hi,
I tried to enter the function but it does not work for me. Could you please look into it?
This is the function i used.
add_filter(‘pre_get_posts’, ‘excludeCat’);
function excludeCat($query) {
if ( $query->is_home ) {
$query->set(‘cat’, ‘-3,-5,-23’);
}
return $query;
}November 12, 2018 at 4:08 am #725064Leo
StaffCustomer SupportYou need to replace this part:
'cat', '-3,-5,-23'with the actual name of the categories you want like to exclude. If there is only one category you want to exclude, then you just need to include one quotation.Let me know if this makes sense 🙂
November 12, 2018 at 4:20 am #725076MÃ¥rten
Yes, i know.
The name of the category is Nyheter. And this is how my function looks like.
add_filter(‘pre_get_posts’, ‘excludeCat’);
function excludeCat($query) {
if ( $query->is_home ) {
$query->set(‘cat’, ‘-nyheter’);
}
return $query;
}November 12, 2018 at 5:53 am #725122Leo
StaffCustomer SupportTry:
add_filter('pre_get_posts', 'excludeCat'); function excludeCat($query) { if ( $query->is_home ) { $query->set('Nyheter'); } return $query; }November 12, 2018 at 6:38 am #725150MÃ¥rten
It does not work either.. Sorry.
November 12, 2018 at 6:55 am #725308Leo
StaffCustomer SupportSorry my mistake.
You need to find the category ID, then replace the -3 below:
add_filter('pre_get_posts', 'excludeCat'); function excludeCat($query) { if ( $query->is_home ) { $query->set('cat', '-3'); } return $query; }November 12, 2018 at 7:42 am #725368MÃ¥rten
Thank you!
Now it works. 😀
November 12, 2018 at 4:31 pm #725727Leo
StaffCustomer SupportNo problem 🙂
July 29, 2020 at 5:21 am #1380387Andrea
I’m trying to make this work, but… Where do I need to include the function? Within the file for the Blog page or is it enough to put in the personalized CSS? Sorry for the basic question. (I suppose I need to modify the file, but before doing something like that, I prefer to ask.)
Many thanks.
Note: maybe it doesn’t work because it was 2018 and something has changed.
July 29, 2020 at 5:52 am #1380427David
StaffCustomer SupportHi there,
that code is PHP, this article explains how to add that to your site:
July 29, 2020 at 5:55 am #1380429Andrea
Thanks, David!
Have you ever considered to change your name in Flash? It’s always surprising how fast you guys are answering… Touché! (And happy with GP Pro!) Never felt so happy with one of my websites.July 29, 2020 at 7:17 am #1380516David
StaffCustomer Supporthaha – we try our best.
Glad to hear you’re happy with GP + GPP – feedback is much appreciated.September 5, 2020 at 2:23 am #1431598Andrea
Hi great guys,
I used the “Code Snippets” plug-in it’s suggested on the page you linked, David.
Many thanks, it worked great.But now I have a strange behaviour in the blog (I don’t know when it started): I cannot see the last posts. And I don’t understand where is the fault. I just added this snippet to exclude 3 categories:
add_filter(‘pre_get_posts’, ‘excludeCat’);
function excludeCat($query) {
if ( $query->is_home ) {
$query->set(‘cat’, ‘-141,-142,-143’);
}
return $query;
}Here is the blog page:
https://s3nzanom3.com/blog-s3nzanom3/As you can see, I have 3 newer posts after the last showed and they are not in the excluded categories. What’s wrong? Do you have any idea?
Note: I posted the same problem on the “WP Show Posts” support page, sorry. I mixed two different errors there. I’m going to comment on that, because this issue seems more related to (my) GP configuration and it’s not related to show posts, as it’s the main blog page.
-
AuthorPosts
- You must be logged in to reply to this topic.