WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I display a gallery shortcode for pages who are children of the page with an ID of 9, and the regular content for pages who aren't? This is what I've tried so far, but it's not working:

<?php global $wp_query; if( (9 == $ $wp_query->post->post_parent ) :?>
    <?php echo do_shortcode('[gallery link="file" columns="1" size="large"]'); ?>
<?php else (); ?>
    <?php the_content(); ?>
share|improve this question
up vote 1 down vote accepted
$thispageid = get_the_ID();
$thispageparent = get_ancestors($thispageid);
if( in_array( 9, $thispageparent ) )
     echo do_shortcode('[gallery link="file" columns="1" size="large"]'); 

reference get_ancestors()

share|improve this answer
Should there be something at the end to close this? Gives an error, says a problem with the line where I have <?php get_footer(); ?> – Phantasmix Aug 3 '12 at 22:39
changed the code a bit. notice $thispageparent = get_ancestors($thispageid); – peteroak Aug 3 '12 at 22:43
also, you've commented on the response above, but i think you meant to write it here :) – peteroak Aug 3 '12 at 22:43
Hi Peteroak, thanks for your help. Reply above was for NVWD, was trying out both solutions :) I switched the shortcode and the_content and it worked for the parent page (shows the_content OK), but the gallery on the child page isn't working. It's the shortcode part. Just to make sure I switched everything around correctly, here's the code <?php $thispageid = get_the_ID(); $thispageparent = get_ancestors($thispageid); if( $thispageparent[0] == 9 ) echo the_content(); else do_shortcode('[gallery link="file" columns="1" size="large"]'); ?> – Phantasmix Aug 3 '12 at 23:01
Ahhh added "echo" before the shortcode bit. Thanks a lot! Your solution is working. – Phantasmix Aug 3 '12 at 23:04
<?php global $wp_query; if ( 9 == $wp_query->post->post_parent ) : ?>
    <?php echo do_shortcode( '[gallery link="file" columns="1" size="large"]' ); ?>
<?php else : ?>
    <?php the_content(); ?>
share|improve this answer
Gives an error, says a problem with this line: 'object_id' => $thispageid, – Phantasmix Aug 3 '12 at 22:38
removed........ – Phantasmix Aug 4 '12 at 1:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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