Displaying Only Certain Tags in Loop

Posted in: Newsmag
Post count: 19

I’m trying to only display certain tags in the loop so I’m using this:

<?php function custom_tags( $query ) {
if (is_admin()){
$query->is_main_query();
return;
}
$query->set( 'tag', "food, recipes, kitchen" );
}
add_action( 'pre_get_posts', 'custom_tags' );

It has been working fine, but I notice this bit of code here makes the whole menu disappear:
$query->set( 'tag', "food, recipes, kitchen" );

How do I stop it from affecting the menu of my wordpress site?

Post count: 6535

Hi

Try this function in functions.php file:


function exclude_category($query) {
	if ( $query->is_home() ) {
		$query->set('tag', 'food, recipes, kitchen'); 
	}
	return $query;
}
add_action('pre_get_posts', 'exclude_category');

Thanks!

  • This reply was modified 10 years by Andrei L..
Post count: 19

the menu still ends up bein hidden at the top for some reaosn

Post count: 6535

Hi

Sorry for that, indeed this code breaks the menu functionality. Try to add the tags in td_data_source.php file lie here: http://screencast.com/t/IXtNXyd6 This should work fine.

Thanks!

Viewing 4 posts - 1 through 4 (of 4 total)
The forum ‘Newsmag’ is closed to new topics and replies.