1

I want to setup a gallery without including it in the content to be able to access it in the theme files.

Currently I use a dummy page that only contains the gallery and isn't reachable from the front end. I would prefer to skip this step and keep the gallery setup on the page where it is displayed.

If the gallery has to be include in the content could I modify the handling of the shortcode in a template specific way and include the gallery before calling the_content();?

4
  • 2
    You can use get_post_gallery($id) - WordPress Codex - Otherwise, you would probably have to make a custom plugin, The Gallery is supposed to work in-tangent with The Content but with the function above, you can strip it out and ignore any extra content.
    – Howdy_McGee
    Commented Mar 13, 2014 at 15:15
  • I currently use get_post_galleries_images but supply the ID from a different page. Would this or get_post_gallery still work if I were to modifiy the shortcode to not return anything when a specific template is used? Commented Mar 13, 2014 at 16:23
  • I'm not sure what you mean by specific template - why not just put get_post_galler() inside that specific template?
    – Howdy_McGee
    Commented Mar 13, 2014 at 16:34
  • That wouldn't remove the gallery from the_content() though, or would it? The page is supposed to display the gallery, than a bit further down the_content() without displaying the gallery again. But I don't want to mess around with the handling of the shortcode to much since on other pages the gallery will have to work the usual way. Commented Mar 14, 2014 at 8:20

1 Answer 1

0

I've used this to display the gallery under the content in a few of my website, or in a custom position, you can manipulate it however you need. The only draw back (sort of) is if there are 2 galleries, it will only work with the first gallery.

<?php
    get_header();

    // Get Post Gallery before The Loop
    global $post;
    $gallery = get_post_gallery($post->ID);
?>

    <?php if(have_posts()) : ?>
        <?php while(have_posts()) : the_post(); ?>
            $content = strip_shortcode_gallery( get_the_content() );                                // remove Gallery Shortcode
            $content = str_replace( ']]>', ']]&gt;', apply_filters( 'the_content', $content ) );    // Resetup Content
            echo $content;                                                                          // Echo Content
        <?php endwhile; ?>
    <?php endif; ?>

    <div id="gallery">
        <?php echo $gallery; ?>
    </div>

<?php get_footer(); ?>

Functions.php

function  strip_shortcode_gallery( $content ) {
    preg_match_all( '/'. get_shortcode_regex() .'/s', $content, $matches, PREG_SET_ORDER );
    if ( ! empty( $matches ) ) {
        foreach ( $matches as $shortcode ) {
            if ( 'gallery' === $shortcode[2] ) {
                $pos = strpos( $content, $shortcode[0] );
                if ($pos !== false)
                    return substr_replace( $content, '', $pos, strlen($shortcode[0]) );
            }
        }
    }
    return $content;
}
0

Your Answer

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

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