Ask your WordPress questions! Pay money and get answers fast! (more info)

How to get images outside the post body to be the thumbnail image

  • SOLVED

Hello,

If you look at this page on my blog: http://www.neliti.com/category/funding/scholarships/

every post has an image thumbnail. This is because every post has an image in the body.

However this post: http://www.neliti.com/return-to-jihad-in-indonesia/ has in image, but there is no image thumbnail when looking at the post in search results or archive pages, for example: http://www.neliti.com/type/voice/.

This is because the image is in the single.php file, but outside the post body.

Can anyone suggest a way to make images in the single.php file of the custom taxonomy "voices" become the post thumbnail?

Thanks for any help :)

Answers (3)

2015-07-08

Hariprasad answers:

Hi,

Is that image is added to posts content, you can use following function to grab the image.

function catch_that_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];
return $first_img;
}


You can call it like $img = catch_that_image();

write a condition for checking the thumbnail image, if it is missing call catch_that_image();

Does that make sense?

Regards,
Hari


neliti comments:

It is not really in the post content. The image is a user-submitted custom field with the key "formimage". It's like this in the single.php file:


<?php do_action( 'bp_before_post_content' ); ?>

<div class="post-content">

<img src="<?php echo get_post_meta($post->ID, 'formimage', true); ?>" alt="<?php echo get_post_meta($post->ID, 'Title', true); ?>" style="height: 228px; padding: 0 20px 20px 20px;" align="right">

<?php $get_ads_single_top = get_theme_option('ads_single_top'); if($get_ads_single_top != '') { ?>

<div class="adsense-single"><?php echo stripcslashes(do_shortcode($get_ads_single_top)); ?></div>

<?php } ?>

<div class="entry-content" <?php do_action('bp_article_post_content'); ?>>

<?php the_content( __('...more &raquo;','mesocolumn') ); ?>

</div>



Hariprasad comments:

Can you also show the archive page code that currently displaying image in archive page?


neliti comments:

The posts that are having the issue are of the custom taxonomy "Type".
If the archive page is changed, won't search results and blogroll still have the problem?

<?php get_header();
$getsinglecat = get_query_var('cat');
if($getsinglecat) {
$singlecat = get_category ($getsinglecat);
$catslug = $singlecat->slug;
$getcategoryslug = get_category_by_slug($catslug);
$cat_id = $getcategoryslug->term_id;
}
?>

<?php do_action( 'bp_before_content' ) ?>

<!-- CONTENT START -->
<div class="content">
<div class="content-inner">

<?php do_action( 'bp_before_blog_home' ) ?>

<!-- POST ENTRY START -->
<div id="post-entry" class="archive_tn_cat_color_<?php echo dez_get_strip_variable($cat_id); ?>">
<div class="post-entry-inner">

<?php do_action( 'bp_before_blog_entry' ); ?>

<?php get_template_part( 'content' ); ?>

<?php get_template_part( 'lib/templates/paginate' ); ?>

<?php do_action( 'bp_after_blog_entry' ); ?>

</div>
</div>
<!-- POST ENTRY END -->

<?php do_action( 'bp_after_blog_home' ) ?>

</div><!-- CONTENT INNER END -->
</div><!-- CONTENT END -->

<?php do_action( 'bp_after_content' ) ?>

<?php get_sidebar(); ?>

<?php get_footer(); ?>


Hariprasad comments:

Its calling another template using <?php get_template_part( 'content' ); ?>

can you show code from content.php?


neliti comments:

<?php
global $postcount;

$post_custom_excerpt = get_theme_option('post_custom_excerpt');
$post_excerpt_moretext = get_theme_option('post_excerpt_moretext');
$post_blog_style = get_theme_option('blogpost_style');

$archive_excerpt = empty($post_custom_excerpt) ? '30' : $post_custom_excerpt;
$excerpt_moretext = empty($post_excerpt_moretext) ? 'Continue Reading' : $post_excerpt_moretext;

$oddpost = 'alt-post'; $postcount = 1;

$feat_size = get_theme_option('feat_img_size');
$feat_size = isset($feat_size) ? $feat_size : 'thumbnail';
$feat_size = apply_filters('meso_thumb_size', $feat_size);

$thumb_w = get_option($feat_size.'_size_w');
$thumb_h = get_option($feat_size.'_size_h');

if (have_posts()) : while (have_posts()) : the_post();
$thepostlink = '<a href="'. get_permalink() . '" title="' . the_title_attribute('echo=0') . '">';
?>

<?php do_action( 'bp_before_blog_post' ); ?>

<!-- POST START -->
<article <?php post_class($oddpost . ' feat-' . $feat_size . ' post-style-'. $post_blog_style); ?> id="post-<?php the_ID(); ?>">
<?php do_action( 'bp_before_post_title' ); ?>

<?php
echo dez_get_featured_post_image("<div class='post-thumb in-archive size-$feat_size'>".$thepostlink, "</a></div>", $thumb_w, $thumb_h, "alignleft img-is-". $feat_size, $feat_size, dez_get_singular_cat('false'), the_title_attribute('echo=0'), false);
?>

<div class="post-right">
<h2 class="post-title entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php get_template_part( 'lib/templates/post-meta' ); ?>
<?php do_action( 'bp_before_post_content' ); ?>
<div class="post-content">
<div class="entry-content"><?php echo dez_get_custom_the_excerpt($archive_excerpt); ?></div>
<?php if($excerpt_moretext == 'disable' || $excerpt_moretext == '') { ?>
<?php } else { ?>
<div class="post-more"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo stripcslashes($excerpt_moretext); ?></a></div>
<?php } ?>
</div>
<?php do_action( 'bp_after_post_content' ); ?>
</div>

</article>
<!-- POST END -->

<?php do_action( 'bp_after_blog_post' ); ?>

<?php ($oddpost == "alt-post") ? $oddpost="" : $oddpost="alt-post"; $postcount++; ?>

<?php endwhile; ?>

<?php else: ?>

<?php get_template_part( 'lib/templates/result' ); ?>

<?php endif; ?>

2015-07-08

Reigel Gallarde answers:

is this a genesis theme?


neliti comments:

I'm not sure? It's the theme "mesocolumn".


Reigel Gallarde comments:

try this...

locate "echo dez_get_featured_post_image" in content.php... might be on line 31...
at the end of that line, there's a false, change that to true...

then

locate "function dez_get_featured_post_image" in lib/functions/theme-functions.php... might be line 604...
find the if in that function that says "if ($default == 'true')"

then change

return $before . "<img width='" . $width . "' height='" . $height . "' class='" . $class . "' src='" . get_template_directory_uri() . '/images/post-default.png' . "' alt='" . $alt . "' title='" . $title . "' />" . $after;

to

return $before . "<img width='" . $width . "' height='" . $height . "' class='" . $class . "' src='" . get_post_meta($post->ID, 'formimage', true) . "' alt='" . $alt . "' title='" . $title . "' />" . $after;


neliti comments:

Almost, but see the attached picture for how it turns out. 2 issues:

1. Regular posts now have a box with the category in it above the title.

2. The image isn't to to the left of the title/excerpt, it's on top of it