conditional shortcode not working

The question:

I’m using a plugin, NextGEN gallery, to display photos. Each gallery from this plugin corresponds to a different sub-page, so I’m using conditional shortcodes to assign the right gallery to it’s corresponding sub-page.
Problem is, it’s not working. I’ve used this exact setup before without issue, which leads me to believe that whatever is causing the problem now is probably something small I’m overlooking. I think a pair of fresh eyes would help out a lot. Thanks in advance.

ETA: I’ve also tried putting the corresponding shortcode directly in the sub-page, which needless to say didn’t work.

gallery.php

<div class="content">
        <h2>PARLOUR GALLERY</h2>
        <div id="stylist-list">
            <h3><a href="index.php?page_id=39" rel="nofollow noreferrer noopener">REBECCA HAEHNLE</a> | <a href="index.php?page_id=41" rel="nofollow noreferrer noopener">CHRIS KELLER</a> | <a href="index.php?page_id=43" rel="nofollow noreferrer noopener">BILLY MALLOY</a> | <a href="index.php?page_id=45" rel="nofollow noreferrer noopener">VERONICA JONES</a> | <a href="index.php?page_id=47" rel="nofollow noreferrer noopener">ABBEY CASTELLANO</a> | <a href="index.php?page_id=49" rel="nofollow noreferrer noopener">QUARTIA FUTRELL</a> | <a href="index.php?page_id=51" rel="nofollow noreferrer noopener">ROOSEVELT OXENDINE</a> | <a href="index.php?page_id=53" rel="nofollow noreferrer noopener">NICOLE LUESCHOW</a> | <a href="index.php?page_id=55" rel="nofollow noreferrer noopener">MEREDITH NOWLIN</a> | <a href="index.php?page_id=57" rel="nofollow noreferrer noopener">MARICEL YALUNG</a> | <a href="index.php?page_id=59" rel="nofollow noreferrer noopener">STEPHANIE BRANDT</a> | <a href="index.php?page_id=61" rel="nofollow noreferrer noopener">JANNAH DIXON</a></h3>
        </div><!-- end stylist-list -->
        <div class="gallery">
                <?php     
                    if( is_page( 39 ) ) {
                      // make your stuff here
                      echo do_shortcode('[nggallery id=1]');
                    } else {
                      // output standard content here
                    }

                ?>
                <?php     
                    if( is_page( 41 ) ) {
                      // make your stuff here
                      echo do_shortcode('[nggallery id=2]');
                    } else {
                      // output standard content here
                    }

                ?>
                <?php     
                    if( is_page( 43 ) ) {
                      // make your stuff here
                      echo do_shortcode('[nggallery id=3]');
                    } else {
                      // output standard content here
                    }

                ?>
                <?php     
                    if( is_page( 45 ) ) {
                      // make your stuff here
                      echo do_shortcode('[nggallery id=4]');
                    } else {
                      // output standard content here
                    }

                ?>
                <?php     
                    if( is_page( 47 ) ) {
                      // make your stuff here
                      echo do_shortcode('[nggallery id=5]');
                    } else {
                      // output standard content here
                    }

                ?>
                <?php     
                    if( is_page( 49 ) ) {
                      // make your stuff here
                      echo do_shortcode('[nggallery id=6]');
                    } else {
                      // output standard content here
                    }

                ?>
                <?php     
                    if( is_page( 51 ) ) {
                      // make your stuff here
                      echo do_shortcode('[nggallery id=7]');
                    } else {
                      // output standard content here
                    }

                ?>
                <?php     
                    if( is_page( 53 ) ) {
                      // make your stuff here
                      echo do_shortcode('[nggallery id=8]');
                    } else {
                      // output standard content here
                    }

                ?>
                <?php     
                    if( is_page( 55 ) ) {
                      // make your stuff here
                      echo do_shortcode('[nggallery id=9]');
                    } else {
                      // output standard content here
                    }

                ?>
                <?php     
                    if( is_page( 57 ) ) {
                      // make your stuff here
                      echo do_shortcode('[nggallery id=10]');
                    } else {
                      // output standard content here
                    }

                ?>
                <?php     
                    if( is_page( 59 ) ) {
                      // make your stuff here
                      echo do_shortcode('[nggallery id=11]');
                    } else {
                      // output standard content here
                    }

                ?>
                <?php     
                    if( is_page( 61 ) ) {
                      // make your stuff here
                      echo do_shortcode('[nggallery id=12]');
                    } else {
                      // output standard content here
                    }

                ?>
        </div><!-- end gallery -->
    </div><!-- end content -->

functions.php

<?php function nggallery( $shortcode ){ echo do_shortcode( $shortcode ); /* NextGen Gallery */ } ?>

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

As a test in the first conditional statement replace your //php comment in the else {} portion of your conditional with an actual echo statement to see whether the conditional is actually working properly,

<?php     
    if( is_page( 39 ) ) {
      // make your stuff here
      echo do_shortcode('[nggallery id=1]');
    } else {
      echo 'It works!';
    }

?>

Go to any of your sub-pages other than 39 of course, and you should see It works! print to screen which then suggests the problem lay more firmly with the plugin.

We need to ensure your conditional is validating properly first..

UPDATE

Seems you have a duplicate question posted here on August 21st which I stumbled across and so to your website and I can see the issue now.

The page id 39 corresponds to this URL,

http://www.yourdomain.com/rebecca-h/

…when navigating to that page you can see the gallery is working perfectly.

However when you are on your gallery page, i.e.,

http://www.yourdomain.com/gallery/

And you use any of the links within your code block,

<a href="index.php?page_id=39">REBECCA HAEHNLE</a>
<a href="index.php?page_id=41">CHRIS KELLER</a>
<a href="index.php?page_id=43">BILLY MALLOY</a>
...shortened for brevity

The URL in which your browser tries to retrieve is,

http://www.yourdomain.com/gallery/index.php?page_id=39

Which for all purposes is incorrect. In fact you’ll notice that you can see the It works! statement echo out to the screen when trying to view Rebecca Haehnle’s gallery which means that the conditional statement, although working, is validating as false instead of true.

Thus we are certainly not on the sub-page like you thought.

Instead, change your links to the following format,

<a href="<?php echo bloginfo('url'); ?>/index.php?page_id=39">REBECCA HAEHNLE</a>
etc...

This will print the top level domain name as your URL http://www.yourdomain.com before /index.php?page_id=XXgiving you a proper URL.

Alternatively you can use the permalink/slug instead of the ugly index.php?page_id=XX business like so,

<a href="<?php echo bloginfo('url'); ?>/rebecca-h/">REBECCA HAEHNLE</a>
etc...


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment