How to set external URL image to post thumbnail (featured thumbnail)

Posted in: Newspaper
Post count: 5

Hello,
I have a code to get external url image to post thumbnail, i’m put it in functions.php

function get_first_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches[1][0];
 
  if(empty($first_img)) {
    $first_img = "/path/to/default.png";
  }
  return $first_img;
}

I need replace function the_post_thumbnail() with above function. Where file to edit?
Thank you!
(Sorry, my English is bad)

Post count: 5

Hello,
Someone help me?

Post count: 6600

Hi,

Is this what you are looking for? This is the function that gets the thumbs for the module http://screencast.com/t/foGHudqKcG

Hope this helps!
Thanks

  • This reply was modified 12 years by Lucian.
Post count: 5

Thank you Lucian.
I found get_image function in file: includes\modules\module_modifier\td_module_blog.php and I edit it.
I’m change line (in this function):
return ''; //the post has no thumb
To
echo '<img src="' . get_first_image() . '" alt="' . the_title(). '" />';
And add this function:

function get_first_image() {
					  global $post, $posts;
					  $first_img = '';
					  ob_start();
					  ob_end_clean();
					  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
					  $first_img = '';
					  if (isset($matches[1][0])) {$first_img = $matches[1][0];}
					  return $first_img;
			}

Results:

/**
     * get_image v010 - modified on wp_010 - returns the featured image of a single post
     * @param $thumbType
     * @return string
     */
    function get_image($thumbType) {
        global $page;

        //show only on the first page when we have posts with pages
        if (!empty($page) and $page > 1) {
            return;
        }

        //do not show the featured image if the global setting is set to hide - show the video preview regardless of the setting
        if (td_util::get_option('tds_show_featured_image') == 'hide' and get_post_format($this->post->ID) != 'video') {
            return;
        }

        //do not show the featured image on gallery post format
        if (get_post_format($this->post->ID) == 'gallery') {
            return;
        }

        //handle video post format
        if (get_post_format($this->post->ID) == 'video') {
            //if it's a video post...
            $td_post_video = get_post_meta($this->post->ID, 'td_post_video', true);
            $td_video_support = new td_video_support();

            //render the video if the post has a video in the featured video section of the post
            if (!empty($td_post_video['td_video'])) {
                return $td_video_support->renderVideo($td_post_video['td_video']);
            }
        } else {
            //if it's a normal post, show the default thumb

            if ($this->post_has_thumb) {
                //get the featured image id + full info about the 640px wide one
                $featured_image_id = get_post_thumbnail_id($this->post->ID);
                $featured_image_info = td_util::attachment_get_full_info($featured_image_id, $thumbType);

                //get the full size for the popup
                $featured_image_full_size_src = td_util::attachment_get_src($featured_image_id, 'full');

                $buffy = '';

                $show_td_modal_image = td_util::get_option('tds_featured_image_view_setting') ; //@todo read this setting from theme panel
                if ($show_td_modal_image != 'no_modal') {
                    //wrap the image_html with a link + add td-modal-image class
                    $image_html = '<a href="' . $featured_image_full_size_src['src'] . '" data-caption="' . htmlentities($featured_image_info['caption'], ENT_QUOTES) . '">';
                    $image_html .= '<img width="' . $featured_image_info['width'] . '" height="' . $featured_image_info['height'] . '" itemprop="image" class="entry-thumb td-modal-image" src="' . $featured_image_info['src'] . '" alt="' . $featured_image_info['alt']  . '" title="' . $featured_image_info['title'] . '"/>';
                    $image_html .= '</a>';
                } else { //no_modal
                    $image_html = '<img width="' . $featured_image_info['width'] . '" height="' . $featured_image_info['height'] . '" itemprop="image" class="entry-thumb" src="' . $featured_image_info['src'] . '" alt="' . $featured_image_info['alt']  . '" title="' . $featured_image_info['title'] . '"/>';
                }

                $buffy .= '<div class="td-post-featured-image">';

                // caption - put html5 wrapper on when we have a caption
                if (!empty($featured_image_info['caption'])) {
                    $buffy .= '<figure>';
                    $buffy .= $image_html;

                    $buffy .= '<figcaption class="wp-caption-text">' . $featured_image_info['caption'] . '</figcaption>';
                    $buffy .= '</figure>';
                } else {
                    $buffy .= $image_html;
                }

                $buffy .= '</div>';

                return $buffy;
            } else {
                echo '<img src="' . get_first_image() . '" alt="' . the_title(). '" />';
            }
            function get_first_image() {
					  global $post, $posts;
					  $first_img = '';
					  ob_start();
					  ob_end_clean();
					  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
					  $first_img = '';
					  if (isset($matches[1][0])) {$first_img = $matches[1][0];}
					  return $first_img;
			}
        }
    }

But it not work. Can you help me fix it!
I want to get first image on post and set it as featured image.
Thank you!

  • This reply was modified 12 years by HanSho.
Post count: 5

Hello,
Someone help me?
Thanks!

Post count: 6600

Hi,

Unfortunately we can’t help you at this time, this is not an easy task and we cannot offer custom work as we work hard on the development, updates, fixes and support.
You can always get a freelancer from sites like: http://studio.envato.com/ if you don’t know how to do it yourself.

Thanks for understanding

Viewing 6 posts - 1 through 6 (of 6 total)
The forum ‘Newspaper’ is closed to new topics and replies.