• Resolved Pete

    (@perthmetro)


    I’m looking for a snippet of code (looked and googled everywhere) that will only display something/anything/html etc if the gallery attached to a post has more than 1 image.

    Thanks heaps

    Love Pete

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    That sounds familiar. Can you provide a link to a post with only 1 image where you don’t want to display something as well as a link to a post with more than 1 image which you do want to display something?

    Also what is the something?

    Thread Starter Pete

    (@perthmetro)

    Good question ??

    I have a website (under development) where I have the featured image displayed above the content as ummm a featured image! I also have the gallery code <?php echo do_shortcode('[gallery link="file"]'); ?> below the content to automatically display if all the images attached to my post.

    Now, if I only attach 1 image it will be the featured image above the content and therefore no need to show the gallery which would be the same image below the content. If I attached 2 images however the gallery would look much more appropriate.

    Clear as mud?

    Thread Starter Pete

    (@perthmetro)

    But it would also be nice to display ‘something’ else as well if there was only 1 image e.g “there is no gallery here suckaaaaa”

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    I have the featured image displayed above the content as ummm a featured image

    Should be doable especially as the featured image HTML is filterable via the the_post_thumbnail(). That filter is my new favorite toy. I’m resisting the urge to make all problems into nails for this hammer. ??

    I’ll take a look tonight but the logic should be

    If there is a gallery in the_content with only one image then show the featured image and don’t display that gallery.

    else display the featured image and the gallery (if the gallery has 2 or more images).

    This is for the front page view?

    Thread Starter Pete

    (@perthmetro)

    Thanks Jan, it’s for a single custom post

    [ No bumping please. ]

    Thread Starter Pete

    (@perthmetro)

    <?php
    $args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'post_parent' => get_the_ID()
    );
    $images = get_posts( $args );
    if( count( $images )  == 1 ){
    ?>
    no gallery
    <?php } else if( count( $images )  > 1 ){ ?>
    Gallery shortcode goes here
    <?php } else { ?>
    There are no photos attached
    <?php } ?>
    Thread Starter Pete

    (@perthmetro)

    whoops wrong post

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to only display something if the post's gallery has more than 1 image’ is closed to new replies.