Is it possible to setup a gallery in the backend without including it in the content?

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.

Read More

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();?

Related posts

Leave a Reply

1 comment

  1. 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;
    }