1

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

  • 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); – pcarvalho Aug 3 '12 at 22:43
  • also, you've commented on the response above, but i think you meant to write it here :) – pcarvalho 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
0
<?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(); ?>
  • Gives an error, says a problem with this line: 'object_id' => $thispageid, – Phantasmix Aug 3 '12 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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