0

I've got a system for post images where I have a specific category's post thumbnail shown in the header, with a cropped version appearing in the blog #preview, and the full size image appearing in the full post.

Images are not appearing in any of the 3 locations, and I'm not quite sure why.

functions.php-

<?php register_sidebar(); if (function_exists('add_theme_support')) { add_theme_support('post-thumbnails'); set_post_thumbnail_size(140,170); } if ( function_exists( 'add_image_size' ) ) { add_image_size( 'custom-image', 440, 265, true ); //(hard cropped) } function ravs_get_custom_image( $featured_img ){ global $post, $posts; $args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $post->ID, 'exclude' => $featured_img ); $attachments = get_posts( $args ); if ( $attachments ) { foreach ( $attachments as $attachment ) { $img = wp_get_attachment_image( $attachment->ID, 'custom-image' ); return $img; } }else{ echo 'Please attach images to your post'; } } ?> 

index.php

<?php echo get_header('cait');;?> <?php echo get_sidebar();;?> <div id="blog"> <div class="preview"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div class="post-image"> <?php echo ravs_get_custom_image( get_post_thumbnail_id () ); ?> </div><!-- end post-image --> <div class="post"> <h2><?php the_title() ;?></h2> <span class="post-preview"> <?php the_excerpt('read more...'); ?> <?php endwhile; else: ?> <p>Sorry, no posts to list</p> <?php endif; ?> </span><!-- end post-preivew --> <p class="post-meta"><span style="font-family: amatic;">>></span>&nbsp;Posted on <?php the_date('M-d-y'); ?>&nbsp;<span style="font-family: amatic;">>></span>&nbsp;<?php the_tags('tags: ', ', ', '<br />'); ?></p><!-- end post-meta --> </div><!-- end post --> </div><!-- end preview --> </div><!-- end blog --> <?php get_footer(); ?> 
2
  • You call ravs_get_custom_image() with an image's post ID, and then you exclude it in the get_posts() call in ravs_get_custom_image(). Why? (Also, you can double-check your post thumbnail ID with something like echo( 'DEBUG: Thumbnail ID=' . get_post_thumbnail_id() . '<br />' );.) Commented May 29, 2013 at 1:06
  • @PatJ Truthfully, I've put all of this together through use of this forum and various tutorials. This is my first attempt at something this complex, and while I am trying to learn along the way it's possible I've missed something. Am I correct in understanding that I should remove 'exclude' => $featured_img from functions.php? (Doing that makes no changes to the site.) Commented May 29, 2013 at 1:36

1 Answer 1

2

Since you appear to be using Featured Images, replace this:

<div class="post-image"> <?php echo ravs_get_custom_image( get_post_thumbnail_id () ); ?> </div><!-- end post-image --> 

with this:

<div class="post-image"> <?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'custom-image' ); } ?> </div> 

References

Codex:
has_post_thumbnail()
the_post_thumbnail()

1
  • 1
    Excellent. Experience has taught me that WordPress handles a lot of this stuff for you, out of the box; sometimes it's just a matter of scrolling through the Codex's Function Reference. Commented May 29, 2013 at 2:01

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.