Support » Fixing WordPress » Captions not working on custom template pages

  • Resolved gluethje


    I’m having a problem with captions not working, and it seems to only be on pages that us a custom template. I just get the raw [caption id="xxx" align="alignleft" width="150" caption="xxx"][/caption] wrapping around the image. You can see an example here:

    The page code I’m using for that page goes like this:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    They seem to work fine on normal posts, as seen here (I need to work on the styling, but they do come through properly):

    Can anyone point me in a direction? I’m about to start banging my head on the desk…I need to get this site wrapped up, but I keep getting tripped up with little things like this. The perils of learning custom theme development on the job, I guess.


Viewing 9 replies - 1 through 9 (of 9 total)
  • Helen Hou-Sandi


    WordPress Dev

    Where is this caption shortcode coming from? It seems to be coming from <?php echo get_post_meta($post->ID, 'Product Images', true); ?> in your code, so I’m guessing the shortcode in the Product Images custom field. If so, you need to use do_shortcode() on that piece of content, as do_shortcode() is only applied to the_content by default. So you’d have <?php echo do_shortcode( get_post_meta($post->ID, 'Product Images', true) ); ?> instead.



    Forum Moderator

    how did you add the ‘captions’?

    the section with the caption and the thumnail images is not from within ‘the_content()’ so however the ‘caption’ code was added, it is not parsed.

    wild guess:
    it might just work if you try:

    <?php echo apply_filters('the_content',get_post_meta($post->ID, 'Product Images', true)); ?>

    That’s right, I did have the image within the_content(), but I changed them to be in a custom field. For some reason I was thinking they were still in the_content()…

    And your solution worked, alchmyth. Thank you so much!

    That was easy.



    Forum Moderator

    you are welcome 😉

    it sounds as if the suggestion by @helen Hou-Sandi could possibly work as well.

    Yes, Helen Hou-Sandi’s suggestion works also, and might be preferable in this case, and I have added it to that page. I need to research the differences between those two methods.

    I already like the do_shortcode method better, because it doesn’t wrap the images that don’t have captions in a <p> tag. It seems that the ‘the_content’ filter probably does a lot more that what I need for this custom field, which is to just pull in the captions properly.

    Thank you both once again. The WordPress community is fantastic!

    Now I’ve got a couple of other problems, though. but I should probably start another thread for one of them.

    1) There is some kind of a dash character appearing to the left of the captions. I cannot figure out where it’s coming from…

    2) I have the Shadowbox plugin installed, but I can’t get it to work with images in custom fields. It only works for images inserted into the editor. Neither of the above methods help with that problem. Always something!

    Helen Hou-Sandi


    WordPress Dev

    Basically, applying all the_content’s filters will also get you things like wpautop, which is where those p tags come from.

    As for your other questions:

    1) The dash character is in the CSS using the :before pseudo element:

    .wp-caption .wp-caption-text:before {
    	content: '\2014';

    2) There’s an option in the Shadowbox plugin to automatically try to capture all of the images in the content, but I guess that only gets applied to the_content. To manually get an image link using Shadowbox, add rel="shadowbox" to the <a> tag. If you want them to show in a continuous gallery, use something like rel="shadowbox[gallery]

    Thank you, Helen. I hand’t noticed the pseudo class on the .wp-caption class.

    And on the shadowbox problem, I disabled the “smart loading” feature and it solved that problem. I did like the idea of smart looading to keep script requests down, but I can live with it.

    I know the rel=”shadowbox” codes, but I don’t want the client to have to type those in.

    Helen Hou-Sandi


    WordPress Dev

    Oh great, I forgot about the smart loading feature. I knew one of those options looked only in the_content 🙂 Always good to automate Shadowbox if possible, WP sometimes chews on manually entering a rel attribute with square brackets in it anyway.

    Glad you got it all sorted out!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Captions not working on custom template pages’ is closed to new replies.