WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Help! How to use the [gallery] shortcode inside a theme template?? (11 posts)

  1. WPChina
    Member
    Posted 5 years ago #

    This is frustrating... I want to put the [gallery] shortcode in a Single Post (single.php) template for my posts so that if a gallery exists, it shows, and if it doesn't then it doesn't display.

    But the BIG problem is that if I simply add [gallery] to a theme template, it is displayed as that text of [gallery] -- I mean it shows no images because WordPress assumes that is just text I have written...

    So my question is how do I add a gallery shortcode to a theme template of Single Post (single.php) so that it works??

  2. Evita
    Member
    Posted 5 years ago #

    how do I add a gallery shortcode to a theme template of Single Post (single.php) so that it works

    You can´t do this. [gallery] is not a template tag. Each gallery has its own ID, created by publishing the post (with a gallery in it).

  3. WPChina
    Member
    Posted 5 years ago #

    Right, but there must be some way to just place [gallery] in the single.php template, and then if the post does have images, they are displayed, and if they don't then they are not displayed. Is that not possible?

  4. Evita
    Member
    Posted 5 years ago #

    Is that not possible?

    Maybe this can be done with some PHP hack, but certainly not with the shortcode.

  5. nast0
    Member
    Posted 5 years ago #

    http://codex.wordpress.org/Using_the_gallery_shortcode

    details how to add shortcode into a template.

    n

  6. WPChina
    Member
    Posted 5 years ago #

    @nast0: Thanks, but that doesn't show how to use it in a theme template. Using the [gallery] just prints out the text [gallery] if placed within a template...

    Anybody have any clues on how to use PHP for this? I'm sure many other users have these needs.

  7. Evita
    Member
    Posted 5 years ago #

    I'm sure many other users have these needs.

    For what?

  8. WPChina
    Member
    Posted 5 years ago #

    @Evita: To make things a little easier... :)

    For example, if we want our blog to always maintain the same general feel with pic in one place, text in another, etc, we can embed the [gallery] within the single.php so that the blog writer doesn't accidentally place it in different points. Plus, if it is embedded in the single.php, we can add further markup around that for design purposes...

    tks!

  9. Evita
    Member
    Posted 5 years ago #

    In really don´t understand.
    The gallery output is (by default) in a div with class "gallery" and you can style this div as you like in style.css - no matter if you put this div manually in a template or automated in a post. The output will always be the same.

  10. nast0
    Member
    Posted 5 years ago #

    It clearly states in that page...

    You can also print a gallery directly in a template like so:
    <?php echo do_shortcode('[gallery option1="value1"]'); ?>

    That's what you're trying to achieve is it not?

  11. mariostella
    Member
    Posted 5 years ago #

    @ Evita
    Why? Ever tried using WP as a CMS where you want to have more control over the content posted especially if you let users write posts? There are a million reasons to want shortcodes (which I personally do not like) outside the loop or at least wherever you need them hard-coded in your template. Just 'cause there are people that have needs you do not have, it does not mean they should not have them. Plus, if you suggest a request is useless, suggest an alternative instead of just saying "For what?"
    This is an old thread, I know, but I still wanted to point this out to you. Many users are judgmental of others..let this be done by moderators. Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic