-
AuthorPosts
-
December 10, 2015 at 3:35 am #700723
Hello X-theme team,
On my site i have three categories. One of them i don’t want to be shown. On your forum i saw the following lines which would do the trick:
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( ‘cat’, ‘-1,-1347’ );
}
}
add_action( ‘pre_get_posts’, ‘exclude_category’ );For me it didn’t work. I also tried to specify the actual page where the blog posts are on. I changed
if ( $query->is_home() into if ($query->is_page(196)
But also this didn’t work.Do you have other solutions?
Thanks,
RichardDecember 10, 2015 at 3:38 am #700732This reply has been marked as private.December 10, 2015 at 4:39 am #700798Hi Richard,
Thanks for writing in!
I tried the code in my local test site and it seems to be working.
You need to replace 1 and 1347 with the categoryid you want to exclude and since you only need one, you can enter just one number.
eg.
function exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '-1' ); } } add_action( 'pre_get_posts', 'exclude_category' );
Change 1 with your category id.
Here is how to locate your category ids
https://community.theme.co/kb/how-to-locate-category-ids/
Hope that helps.
December 10, 2015 at 7:06 am #700934Sorry i copy pasted the code from the forum and forgot to mention that i already had changed
$query->set( ‘cat’, ‘-1,-1347’ );
into my category’s id so:
$query->set( ‘cat’, ‘-11’ );But the category (reviews) still shows.
I als tried it with another of my categories (blogs; id 4) but also that category doesn’t disappear. And with every change, i first emptied the browsers cache.
Plus all of sudden my wp adminbar is gone when viewing the site.
I have checked Cornerstone and there i have “show wordpress toolbar” enabled
And i also have checked the user settings under which i login and also there i have “show toobar while viewing site” enabled.PS: i left the jQuery untouched so you can see the current settings.
Kind regards
RichardDecember 10, 2015 at 7:06 am #700936This reply has been marked as private.December 10, 2015 at 7:11 am #700940A follow up to my message: you can forget the toolbar issue. I t has something to do with a maintenance plugin. For when i disable it, the toolbar is back. So i will have to sort that problem out at the other party.
December 10, 2015 at 11:46 am #701279Hi Richard,
Would you mind providing us with your FTP details so that we can safely revise and test the code?
Thank you.
December 11, 2015 at 1:40 am #702281This reply has been marked as private.December 11, 2015 at 2:14 am #702308Hi,
Thank you for providing your ftp login.
Upon checking, I can see that you have added the code in the wrong place.
Kindly remove the code in your custom > javascript box in the customizer that reads.
function exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '-11' ); } } add_action( 'pre_get_posts', 'exclude_category' );
Then add it in your child theme’s functions.php file instead (wp-content/themes/x-child/functions.php).
Hope that helps.
December 11, 2015 at 6:54 am #702604Okay, i thought that all jQuery had to go in the javascript box in the customizer.
I already have some jQuery there… should i move that over as well?
And about the exclude jQuery: it partly worked. I now haven’t got the review posts (individually) anymore. But i still have them in the category list and in the archive list.
See attached image. And i want them also gone overthere… Do i need an additional jQuery for that?thanks again
RichardDecember 11, 2015 at 6:55 am #702606this is the image in which you can see reviews is still under categories
December 11, 2015 at 11:39 am #702977Hi there,
You can use a plugin like Ultimate Category Excluder (https://wordpress.org/plugins/ultimate-category-excluder/) for this. Once you have installed the plugin, you can head over to Settings -> Category Exclusion section and then select the categories that you want to hide.
Hope that helps.
December 12, 2015 at 2:36 am #703790Hi there,
i had already tried that but didn’t work. But i just tried again cause i couldn’t remember how the plugin settings were. But looking at it, there aren’t too many settings, you can’t either enable or disable categories. So i can hardly do anything wrong… hahaha but again: i doesn’t do anything! Maybe that is because the plugin isn’t updated for over two years.
December 12, 2015 at 3:16 am #703802Hello There,
To exclude the review posts from your category and other archive pages, please update your code and use this code instead in your child theme’s functions.php file:
function exclude_category( $query ) { if ( ( $query->is_home() || $query->is_archive() ) && $query->is_main_query() ) { $query->set( 'cat', '-11' ); } } add_action( 'pre_get_posts', 'exclude_category' );
We would loved to know if this has work for you. Thank you.
December 12, 2015 at 3:40 am #703825No sorry, didn’t change a thing.
In the mean time while searching the internet i stumbled upon a code which does the job but to a certain extend. I’m not a coder but maybe you guys (and girls?) understand this code and maybe can do some additions to it.
I added the following code at the end of my child’s php file:
add_filter( ‘widget_categories_args’, ‘wpsites_exclude_widget_category’, 11 );
add_filter( ‘widget_categories_dropdown_args’, ‘wpsites_exclude_widget_category’, 11 );
function wpsites_exclude_widget_category( $cat_args ) {
$cat_args[‘exclude’] = array(’11’); //the categories removed
return $cat_args;
}Under categories, it’s gone… yeah!!! But the reviews posts are still under Archive.
But as i said, you prolly understand the code and know how to add “archive” to it…. or is that too simple to say???? 😉best regards
Richard -
AuthorPosts