Event Template for Custom Taxonomy

This topic contains 3 replies, has 2 voices, and was last updated by  Stephen Harris 7 years, 1 month ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #26561

    I’ve added a custom taxonomy to events called speaker so we can display a list of speakers for events.
    In general, it’s working fine except I’m unable to get the taxonomy archive to display the events as “events”.

    I believe this is because the plugin’s templating functions are only looking for predefined contexts:
    ‘event’, ‘archive’, ‘event-venue’, ‘event-category’ or ‘event-tag’. I’m not seeing any hooks to modify that.

    Is there any way to accomplish this?

    tadpole
    #26568

    The best way to handle this is to create your own taxonomy-speakers.php template. (Your theme may have a pre-existing appropriate template for you to copy).

    That template, will contain (though it may do this by including other template files) a loop such as:

    <?php if ( have_posts() ) { ?>
    
        ...
    
        <?php
        while ( have_posts() ) : the_post();
            ...
        endwhile;
        ?>
    
        ...
    
    <?php }; 
    

    Inside that while loop you can use:

    eo_get_template_part( 'eo-loop-single-event' );
    

    Or you could even replace the whole if/else with:

    eo_get_template_part( 'eo-loop-events' );
    

    Here’s an example but be warned that you may have to edit that to match your theme if you wish to use that. Copy it to your theme, rename, and edit it. Please note that template file is not used by the plug-in by default.

    Stephen Harris
    #26573

    Thanks for the reply. And, sorry I wasn’t clear. This isn’t for a theme, it’s plugin. I’m creating a plugin
    that adds speakers.

    I’m looking to tie into the existing templating structure so that I don’t need to recreate templates.
    The taxonomy archive should be identical to Event Categories or Event Tags.

    tadpole
    #26585

    I see, in that case the best I can do is point to you how the plug-in does it (there are no filters to ‘extend’ the behaviour to another taxonomy – so you’ll need to just have some something similar in parallel).

    • Hook into the template_include filter (later the better, but most of the time this isn’t a concern)
    • Check for is_tax( 'speakers' ) – if this returns false, just return the passed template (i.e. do nothing)
    • Next (optionally) check the name of the passed template file. If it matches taxonomy-speakers.php, then the theme has an appropriate template, so you should probably just use that.
    • Otherwise you can a) include a template file from your plug-in b) Try to use the theme’s page.php and insert the taxonomy archive as content.

    Event Organiser will do (b) if ‘theme compatibility’ is enabled, (a) if “template handling” is enabled, and will do nothing otherwise.

    (a) is the most simplest you just return the path to a template file from your plug-in. However, it’s quite unlikely that this will work with most themes, because your template file has to act as the template for the entire page and it’s impossible to match the HTML structure of all themes.

    (b) Is more likely to work but involves tricking WordPress into thinking its serving a single page. This is all done here: https://github.com/stephenharris/Event-Organiser/blob/develop/includes/class-eo-theme-compatability.php#L156-L241

    Notice how the content of the page is generated (ignoring the header or $precontent):

        ob_start();
        eo_get_template_part( 'eo-loop-events' );
        $content = ob_get_clean();
    

    It is just using the eo-loop-events.php template.

    You are probably best off just copying that class wholesale, renaming it and going through cutting out the bits you do not need. (Keep in mind functions like eventorganiser_is_event_template() will not work for custom taxonomies.)

    Stephen Harris
Viewing 4 posts - 1 through 4 (of 4 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.