• I’m wondering if there is a way to insert values into the get_the_post_thumbnail function. Currently I’m using this line:

    <?php echo get_the_post_thumbnail( $id, ‘medium’, $attr ); ?>

    Obviously this returns the <img> for the given post within my loop. Let’s say that looks like this:
    <img src=”/uploads/image-1.jpg” alt=”Test Image” />

    What I want to do is insert a value into that <img> string, while still using the get_the_post_thumbnail function. Is it possible?

    I’m working with advanced captions for the jQuery Orbit slider (http://www.zurb.com/playground/orbit-jquery-image-slider).

    Help! This has gotta be possible…

Viewing 7 replies - 1 through 7 (of 7 total)
  • try for example:

    <?php echo get_the_post_thumbnail($id, 'medium', array('value' => 'whatevervalueyouneed')); ?>

    http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

    Thread Starter docbrandes

    (@docbrandes)

    Still not working for me. Here’s my code:

    [please read http://codex.wordpress.org/Forum_Welcome#Posting_Code for posting code]

    <div id=”featured”>
    <?php $tout_query = new WP_Query(‘cat=&showposts=5’); ?>
    <?php while ($tout_query->have_posts()) : $tout_query->the_post(); ?>
    “><?php echo get_the_post_thumbnail( $id, ‘medium’, array(‘data-caption’ => ‘#postTitle’)); ?>
    <?php endwhile; ?>
    </div>

    <span class=”orbit-caption” id=”postTitle”><?php the_title(); ?></span>

    The span at the bottom uses the postTitle id, and inserts a caption into the image (in this case I’m calling the post title as the caption).

    It works if I don’t use get_the_post_thumbnail and instead use a basic <img> tag as follows:

    <img src=”/uploads/test-image.jpg” data-caption=”#postTitle” />

    …but this is in a loop, so I want it to pull dynamically. Any other ideas for inserting that “data-caption=’#postTitle'” into get_the_post_thumbnail?

    this code:

    <?php echo get_the_post_thumbnail( $id, 'medium', array('data-caption' => '#postTitle')); ?>

    for instance, in my local test site, outputs as expected:
    <img src="http://localhost/wordpress/wp-content/uploads/2011/07/image.jpg" class="attachment-medium wp-post-image" alt="image" title="image" data-caption="#postTitle" width="225" height="137">

    what is the html ouptput if you use the functions in your code?
    can you post a link to a post/page where you are using this code?

    Thread Starter docbrandes

    (@docbrandes)

    Thanks alchymyth, it seems to be outputting correctly:

    <img width=”600″ height=”450″ src=”http://www.theperfectlyhappyman.com/uploads/wychwood-king-goblin-600×450.jpg&#8221; class=”attachment-medium wp-post-image” alt=”Wychwood King Goblin” title=”Wychwood King Goblin” data-caption=”#htmlCaption” />

    The demo page I’m working on can be found at http://www.theperfectlyhappyman.com/orbit-demo

    If it’s outputting correctly, I’m wondering if there’s something amiss with the Orbit plugin? Those captions just will not show up!

    it seems that the ID needs to be unique; possibly add the post-id into it:
    <?php echo get_the_post_thumbnail( $id, 'medium', array('data-caption' => '#postTitle'.$post->ID)); ?>

    also, it seems that you need to output the caption texts in a loop, so each gets its unique ID as well;
    example:

    <?php $tout_query = new WP_Query('cat=&showposts=5'); ?>
    <?php while ($tout_query->have_posts()) : $tout_query->the_post(); ?>
    <span class="orbit-caption" id="postTitle<?php echo $post->ID; ?>"><?php the_title(); ?></span>
    <?php endwhile; ?>

    (totally untested)

    Thread Starter docbrandes

    (@docbrandes)

    Thanks again … it looks like that kicks out the code exactly has it should. However I’ve discovered that the slide captions break when they have anchor tags surrounding the images. That’s unfortunate…

    Great stuff. I was trying to figure this out on my own, and I have very little knowledge of PHP. I was able to get the images to show in the loop correctly but I couldn’t get the captions to show up properly. They show up the right way now. Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘get_the_post_thumbnail hack’ is closed to new replies.