WordPress.org

Support

Support » Themes and Templates » [Resolved] Help! How to use the [gallery] shortcode inside a theme template??

[Resolved] Help! How to use the [gallery] shortcode inside a theme template??

  • WPChina

    @wordpresschina

    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??

Viewing 10 replies - 1 through 10 (of 10 total)
  • 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).

    WPChina

    @wordpresschina

    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?

    Is that not possible?

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

    http://codex.wordpress.org/Using_the_gallery_shortcode

    details how to add shortcode into a template.

    n

    WPChina

    @wordpresschina

    @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.

    I’m sure many other users have these needs.

    For what?

    WPChina

    @wordpresschina

    @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!

    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.

    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?

    @ 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.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Resolved] Help! How to use the [gallery] shortcode inside a theme template??’ is closed to new replies.
Skip to toolbar