Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

2 Answers 2

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"]'); 
else
    the_content();

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

 
discard

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.