0

I have a custom post 'events'. For each post there is a gallery attached. On a main galley page i am displaying featured images from each post . Now i want that when person clicks on this featured image , he should be taken to a new template where images from the embedded gallery of the post are displayed using get_post_gallery(). I can't use permalink because it will take me to the 'single-event'.

How can i do it ? Please help me out.

5
  • You need to give more details. How is the gallery attached? What is get_post_gallery()? ......
    – cybmeta
    Jul 29, 2014 at 11:30
  • I attached the gallery using <add media> at the top of main content area . Then i created the the gallery. get_post_gallery is the method of getting the gallery of the current post i am using in single-events.php to get and display the gallery. Using get_post_gallery is not necessary if there is some another way.
    – terminator
    Jul 29, 2014 at 11:40
  • I didn't know about 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?
    – cybmeta
    Jul 29, 2014 at 11:51
  • of course you are right . that is exactly what ii want to do
    – terminator
    Jul 29, 2014 at 12:11
  • Any ideas how to do so
    – terminator
    Jul 29, 2014 at 15:02

1 Answer 1

0

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".

13
  • THANKS A LOT FOR HELPING ME OUT. BUT TH THING IS ITS GOING OVER MY HEAD. I copied all of the above code in functions.php What href="" do i need to give to the featured images on the gallery page
    – terminator
    Aug 1, 2014 at 7:19
  • If you use the first way, you must use URL like mysite.com/event/a-event-post/gallery/1 (with some value after "gallery"). If you use the second way you can use mysite.com/event/a-event-post/gallery/ (without any value after "gallery"). And don't forget to flush the rewrite rules of your site.
    – cybmeta
    Aug 1, 2014 at 9:22
  • I am using the second method.Now do i need to just add mysite.com/event/a-event-post/gallery/ to the href of feature images on gallery or page .
    – terminator
    Aug 1, 2014 at 9:51
  • Yes, that is. And remember to create the single-event-gallery.php template file. If my answer fix your problem and help you, please mark it as accepted. Thank you.
    – cybmeta
    Aug 1, 2014 at 12:24
  • i used href="localhost/basswaves/a-event-post/gallery" but it gives me error object not found. i created the single-events-gallery.php . 'events ' is the custom post
    – terminator
    Aug 4, 2014 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.