Archived topic
Dispatch theme: Posts showing up twice on homepage
26 replies · Started by Malin on August 1, 2021
Hi again,
No, got that Parse error again...
:-)
I updated Toms code here:
Try that :)
Hi,
Great! Thank you, now the duplicates stopped. Well done!
Now, the next step, is it possible to remove certain categories, like Events listings? I.e. the categories here:
https://www.alternativephotography.com/community/events-listings/
That would be awsome!
Thats great - glad we got there - i think thats going to come in very useful :)
So for the main post listing you can exclude categories using this function:
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-xx' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
You need to change the xx to the ID of the Category, you can get the Category ID by editing the Category and looking in the URL it will say ID=66 or something.
Make sure you keep the - minus. So if the ID is 66 this line will be:
$query->set( 'cat', '-66' );
Hi David,
Thank you for your relentless help. Two questions:
1. Do I add this snippet after the other code you supplied, or do i do a new snippet with this code?
2. How do I exclude more than one category, for example 66 and 67? Like this?
$query->set( 'cat', '-66', '-67' );
Thank you again!
Hi there,
for #1,
It's best to create a new one and write a label on it if it has a different purpose.
For #2:
Use array function for this.
Example:
$query->set( 'cat', array( '-66', '-67' ) );
Works like a dream! Thank you so much to all of you: David, Tom and Elvin for helping me out! Very much appreciated!
You're welcome.
I would like to say thank you to you, for your patience and helping us to achieve this solution :)
Ah, I was maybe a little too quick. Posted another post today, and it actually ended up in the list below the first 4. Any ideas?
Should it have appeared in the Top 4 Grid ?
Doh! Sorry, my bad! That particular post is not supposed to be in the top 4. Apologies, perhaps we can delete my comment and this one not to confuse others who may benefit from reading this thread. Thank you and sorry for the confusion.
No problems :) Glad to hear its ok!