WordPress.org

Ready to get started?Download WordPress

Forums

Gallery ID Shortcode No Longer Working in 3.5 (6 posts)

  1. trivum
    Member
    Posted 1 year ago #

    Hi,

    It seems that in 3.5 when you try to insert a gallery into a template file with the gallery id, it no longer works.

    Previously, for example, this would work:

    <?php echo do_shortcode('[gallery id="123"]'); ?>

    Now, however, because the shortcode has been changed to list out the individual images, referring to the gallery as a whole doesn't work anymore.

    The advantage of still having a reference to the entire gallery and not individual images in this case is that you can go in and edit the gallery in the visual editor without digging back into the template code. Just update the gallery as you like, and it's updated everywhere you inserted that gallery in your template files.

    Does anyone know if this is just a glitch, or is this how it will be going forward?

    According to this page (which seems to have been updated for 3.5 -- at least partly), referring to the entire gallery should still work.

  2. Tara
    Member
    Posted 1 year ago #

  3. trivum
    Member
    Posted 1 year ago #

    Thanks, but nothing there seems to address this issue.

  4. trivum
    Member
    Posted 1 year ago #

    Anyone with ideas on this? Thanks.

  5. Tara
    Member
    Posted 1 year ago #

  6. galeroy
    Member
    Posted 10 months ago #

    I figured out a work-around for this:

    Please note that this fix worked for me only under the conditions below and I haven't tested this under any other conditions:
    WordPress version: 3.6.1.
    Theme: Responsive
    Hosting Environment: Linux
    Template where I embedded my code stub: header.php
    My Active Plug ins: "File Un-Attach", "Responsive Add Ons"

    1. Create a blog post but don't add any images to it. You initially want to create up an empty 'shell' post just to get the post ID for your gallery.
      The easiest way I know to get the post ID is after you click on "Publish" you'll see a "Get Shortlink" button near the top of the page. Click on that and the last number in the url is the post ID.
    2. Add this code to your desired template file.
      <?php
          $gallery_shortcode = '[gallery id="' . intval( 176 ) . '" columns="5" orderby="rand"]';
          print apply_filters( 'the_content', $gallery_shortcode );
      ?>

      Things to note:
      --> 176 = the post number for my gallery. Substitute your own post number in place of mine.
      --> Additional output options can be added before the last "]". It's a good to set the number of columns because if you don't set anything, the default is 3.
      --> The text 'the_content' doesn't need to be changed. Leave as is.

    3. For my use case, I only wanted a gallery to be displayed at the top of my home page. I didn't want it to appear on any other pages or in any blog posts. This means that for my use case (and if this is your use case) as odd as it may sound, it's not necessary to actually create a gallery in your post. You only have to add images to your media library and then 'attach' them to the blog post that you set up.
    4. WordPress quirk: It's currently only possible to **attach** a media item to a post from the media library. The only way to **un-attach** a media item from a post is to delete either the media item or the post. I found this to be a pretty good plugin that adds attach and un-attach links to the right of items in your media library: http://wordpress.org/plugins/file-un-attach/
    5. Since:
      a) I only wanted the gallery to be displayed at the top of my homepage
      b) but didn't want the actual blog post to be displayed

      I added the following CSS to my child theme:

      /* Hide the blog post associated with the gallery. */
      #post-176 {
      	display: none;
      }
      
      /* Only display the gallery on the home page.  Hide the gallery on all other web pages. */
      .page-id-23 #gallery-1,
      .page-id-22 #gallery-1,
      .page-id-21 #gallery-1,
      .page-id-2 #gallery-1{
          display: none;
      }
    6. If your use case is different from mine and you want to display your gallery *and* blog post - the cool thing is that you can just and add text and images to your post - and those images can be separate from your gallery images. Just dis-regard the CSS above.

    Hopefully this will at least get you started. You'll need to add other CSS styles to suit your needs.

Topic Closed

This topic has been closed to new replies.

About this Topic