WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] single.php - Using IF/ELSE on post_custom field to enable/suppress auto-gallery (4 posts)

  1. James Adamthwaite
    Member
    Posted 1 year ago #

    In my single.php, I have the following:

    <?php the_content(); ?>
    <?php echo do_shortcode('[gallery size="large"]'); ?>

    My blog is essentially a photo blog, so I just want every post to have a large-size gallery after the content. That way, I don't have to put <img> tags in my post text, I can just use the post text for a description of the photos and then any photos attached to the post just follow the content. It's working out GREAT for 98% of my posts.

    On the rare occasion, I want to stop that from happening, so I'm thinking all I need is to use a post_custom field and call it 'nogallery' and set that to 1 when I don't want that to happen.

    Something like:

    <?php the_content(); ?>
    IF
    POSTCUSTOMFIELD called "nogallery" = 1, THEN DO NOTHING
    ELSE
    <?php echo do_shortcode('[gallery size="large"]'); ?>

    I'm not a PHP guru, so all of my attempts at crafting this IF/ELSE statement have failed.

    Any help out there? THANKS!

  2. shadeypwns
    Member
    Posted 1 year ago #

    <?php if (get_post_meta($post->ID, 'nogallery', true) ) { ?>
    	<?php the_content(); ?>
    <?php } else { ?>
            <?php the_content(); ?>
            <?php echo do_shortcode('[gallery size="large"]'); ?>

    This might work. I believe the putting the custom field "nogallery" with any value after it will = true. If there is no custom field called "nogallery" then it returns false and prints the gallery.

  3. James Adamthwaite
    Member
    Posted 1 year ago #

    You're my new best friend. Thank you SOOOO much!

    I just needed to add the extra bit at the end so I didn't break the loop:

    <?php if (get_post_meta($post->ID, 'nogallery', true) ) { ?>
    	<?php the_content(); ?>
    <?php } else { ?>
            <?php the_content(); ?>
            <?php echo do_shortcode('[gallery size="large"]'); ?>
    <?php } ?>

    :)

  4. shadeypwns
    Member
    Posted 1 year ago #

    Ah my apologies for forgetting the closing bracket XD I'm glad it worked for you!

Topic Closed

This topic has been closed to new replies.

About this Topic