I think one good possibility is to add a endpoint to permalink for galleries and then check the template to include in the template_include
filter:
add_action('init', function() {
add_rewrite_endpoint( 'gallery', EP_PERMALINK );
});
add_filter( 'template_include', function($template) {
if ( get_query_var( 'gallery' ) ) {
return get_stylesheet_directory() . '/single-event-gallery.php';
}
return $template;
});
Now, create the file single-event-gallery.php
and put it in your theme folder. You can copy the content of single-event.php
and replace the_content()
with:
if ( get_post_gallery() ) {
echo get_post_gallery();
} else {
echo "No gallery found";
}
You must use URLs like mysite.com/event/a-event-post/gallery/1
("1" is a random value, any value after "gallery/1" is valid).
I tink it is a simple, fast and easy solution but with these problems:
- It adds the endpoint rewrite rules to all permalinks
- It needs some value after
gallery
in the URL
To avoid that problems we can define a custom rewrite rule only for "event" post type and define a custom query var to use in the template_include
fiter.
add_action( 'init', 'cyb_event_gallery_rewrite_rule' );
function cyb_event_gallery_rewrite_rule() {
add_rewrite_rule( '^event/([^/]+)/gallery/?$', 'index.php?event=$matches[1]&gallery=yes', 'top' );
}
add_filter( 'query_vars', 'cyb_register_gallery_query_var' );
function cyb_register_gallery_query_var( $vars ) {
$vars[] = 'gallery';
return $vars;
}
add_filter( 'template_include', function($template) {
if ( get_query_var( 'gallery' ) ) {
return get_stylesheet_directory() . '/single-event-gallery.php';
}
return $template;
});
Now you can use URL like mysite.com/event/a-event-post/gallery/
. You can generate the URL inside the loop of parent post with (or similar, depends on your permalink settings):
$gallery_url = get_permalink() . 'gallery/';
Note: after you apply this code you need to flush the rewrite rules of your site. You can do that visiting the permalinks settings page and clicking in "save".
get_post_gallery()
? ......get_post_gallery()
, sorry, now I know what it is. So, the gallery is not attached, it is embeded. And what you need is a separate template to display the gallery embeded in the post? Am I right?