Show events on homepage

Hello,

I am using the Events Manager plugin (Events Manager – WordPress plugin | WordPress.org)

I am wanting to have the events display as posts on my homepage in the Sparkling theme.

The plugin I’m using is catered for this, as the events are created as posts, just in a separate table in the database.

How would I amend my query in the Sparkling theme to include the events?

Hi @jdgmedia,

I hope you are well today and thank you for your question.

You have to develop custom code using the action hook pre_get_posts and set the custom post type of it to your events plugin post type so your code will be as following.

function search_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
      $query->set('post_type', 'event');
  }
}

add_action('pre_get_posts','search_filter'); 

You can also use the following WordPress functions in the above code condition to check whether the displayed page is home page or front page so that other pages won’t be affected by this code.

https://codex.wordpress.org/Function_Reference/is_front_page

You can use the developed custom code on your site by adding it in the functions.php file of your child theme or developing small plugin and adding the code in it.

Best Regards,
Movin

Hi Movin,

Thanks for that.

I have managed to get the locations and events displaying on the homepage.

However, for some reason the locations display the full post? And not just the excerpt (or the bit before the read more line) like the events do?

Where might I be going wrong?

Hi @jdgmedia,

Is the locations custom post type?

Could you please share me the code that you have used to display it?

Please make sure you have selected “Shoe post excerpts” option in the Sparkling theme customization settings on the following path as shown in the attached screenshot.

Admin Area -> Appearance -> Customize -> Sparkling Options -> Content Options

Best Regards,
Movin

Hi Movin,

That box is ticked.

Locations is a custom post type.

So is events.

However although events works in that it only shows the excerpt, I can’t seem to get it where locations only shows the excerpt! Which is frustrating.

You can see on my site: jdgmedia.co.uk

I have checked that post excerpts is ticked as you suggested.

Many thanks
James

Hi James,

I could confirm it on your site but to troubleshoot it as requested in the previous reply Could you please share me the code that you have used to display it?

Regards,
Movin

I think it’s this part of the functions.php

add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

	if ( ( is_home() && $query->is_main_query() ) || is_feed() )
		$query->set( 'post_type', array( 'post', 'event', 'location', 'movie', 'quote' ) );

	return $query;
}

//prevents formatting on the homepage if the events in homepage mod is used
function my_em_homepage_eventformats(){
	if(is_home()){
		add_filter('option_dbem_cp_events_formats',function(){return false;});
	}
}
add_action('template_redirect','my_em_homepage_eventformats');

Hi James,

Thanks you for sharing the code but the code that you have shared is not sufficient to troubleshoot the issue.

Could you please share me the whole child theme code or the plugin code where you have added the developed custom code.

Best Regards,
Movin

This is the only custom code I have added?

Hi @jdgmedia,

Could you please try using the attached child theme of Sparkling theme that contains some custom code?

Also please make sure that you have not added any content in the excerpt field of locations post type as described on the following page.

Best Regards,
Movin

Ah that’s brilliant, thanks very much!

I’ve always struggled to understand child themes.

I have made the odd change to the Sparkling theme - should I now move these changes in to the child theme?

For instance, I’ve added a custom element under the slider.

I notice there is an update for the Sparkling theme due.

Hi @jdgmedia,

You are most welcome here :slight_smile:

You should not make any changes in the Sparkling theme, instead you should make changes in the theme using child theme because if you make changes in the Sparkling theme file then you have to make these changes again after theme updation as changes made in the theme files get lost on theme updation which is not the case with child theme therefore please move any changes you have made in the Sparkling theme from it to the child theme.

We are always enhancing our themes and releases updates of it frequently but don’t worry these updates wont affect your child theme.

Cheers,
Movin

So if I make a change to say index.php, do I just upload the changed version to my child theme?

Thanks again.

Hi @jdgmedia,

So if I make a change to say index.php, do I just upload the changed version to my child theme?

Yes you are right. Find more information about it on the following page.

https://codex.wordpress.org/Child_Themes

Cheers,
Movin