Creating a Simple WordPress ‘image.php’ Template

Filed under: Design, WordPress - September 29th, 2008

With 2.5+s new file upload feature creating links to pages with images can sometimes cause issues with a WP site, especially if your default template (single.php) is pre-styled for other use.

To play it safe all you need to do is create a file named ‘image.php’ in your theme directory and then remember to use the wp_get_attachment_image tag.

I stripped this code from the default WP theme and modified slightly:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<h2 class=”title”><?php the_title(); ?></h2>

<div style=”text-align:center;”>

<p class=”attachment”><a href=”<?php echo wp_get_attachment_url($post->ID); ?>”><?php echo wp_get_attachment_image( $post->ID, ‘medium’ ); ?></a></p>
<div class=”caption”><?php if ( !empty($post->post_excerpt) ) the_excerpt(); // this is the “caption” ?></div>

</div>

<div class=”clear”></div>

<?php endwhile; endif; ?>

Remember to add this between the loop of course.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment




powerd by WordPress, dummy!