• Resolved Demonhood

    (@demonhood)


    I posted this in a reply, but thought it needed its only topic.

    I’m grabbing the post_thumbnail for a specific post. I’m then trying to display this as the background image for a div. The problem is that the call for the thumbnail also pulls in the width/height and alt tags. Stuff I don’t need (and that throw’s off the inline css). How can I strip that?

    <?php $posts=query_posts('page_id=22');
    if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <?php
    $thumbnail = '';
    if (function_exists('has_post_thumbnail')) {
        if ( has_post_thumbnail() ) {
             $thumbnail = get_the_post_thumbnail($post->ID,'featured');
        }
    }
    ?>
    
    <?php endwhile; endif; ?>
    <div style="background: url(<?php echo $thumbnail; ?>) no-repeat;>
    <h3><?php the_title(); ?>:</h3>
    <h4>Player's Name!</h4>
    </div>

    Which outputs this:

    <div style="background: url(<img width="292" height="260" src="http://www.mydomain.com/wp/wp-content/uploads/2010/04/player-featured-292x260.jpg" class="attachment-featured wp-post-image" alt="" title="player-featured" />) no-repeat;>
    <h3>Featured Skater:</h3>
    <h4>Player's Name!</h4>
    </div>

    So if I could just easily strip everything before the http of the image and everything after, I’d be good to go.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Michael

    (@alchymyth)

    maybe this similar thread might help:
    http://ww.wp.xz.cn/support/topic/400728?replies=15

    Thread Starter Demonhood

    (@demonhood)

    Interesting thread.
    So it seems that there is no built-in mechanism in WP to do this. Especially since, like that poster, I am using a post_thumbnail of a non-default size.

    So, anyone proficient enough with PHP to know how to strip out just the src location for an image? All the php code I’ve tried online has failed to produce results.

    Michael

    (@alchymyth)

    <img width="292" height="260" src="http://www.mydomain.com/wp/wp-content/uploads/2010/04/player-featured-292x260.jpg" class="attachment-featured wp-post-image" alt="" title="player-featured" />

    if the above is the output of echo $thumbnail;
    then you could try:

    preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $thumbnail, $matches);
    $thumb_url = $matches [1] [0];
    echo $thumb_url;

    or applying two explode funtions to your string:

    $thumb = explode('src="',$thumbnail);
    $thumb = explode('"',$thumb[1]);
    $thumb_url = $thumb[0];
    echo $thumb_url;

    probably not the most elegant solutions – but the good thing is, that the result in the browser does not show how it is done 😉

    Thread Starter Demonhood

    (@demonhood)

    The explode worked perfectly, many thanks.
    After much effort, the post_thumbnail finally proves useful. Yay.

    Many Many Thanks alchymyth and Demonhood ..What works fantastic for a thickbox link i.e. view larger for post thumb:

    <?php
    $thumbnail = '';
    if (function_exists('has_post_thumbnail')) {
    if ( has_post_thumbnail() ) {
    $thumbnail = get_the_post_thumbnail($post->ID,'large');
    $thumb = explode('src="',$thumbnail);
    $thumb = explode('"',$thumb[1]);
    $thumb_url = $thumb[0];
    }
    }
    ?>
    <a href="<?php echo $thumb_url; ?>" class="thickbox">+ View larger</a>
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Strip Image Attributes from my Variable’ is closed to new replies.