How to create a default image for the Unstandard theme

October 5th, 2008

Two tweaks for the Unstandard theme:

How to specify a default image for lead_image and secondary_image and
How to make the home page show posts from only one category.

I know, this is another off-topic post but I’ve had so many people write in to me wanting to know about the tweaks I’ve been doing for this theme that I just thought I’d put it out there.
[grin] Maybe it’s how I’m going to do some more ‘attraction’ of readers???

Anyway, I’ve been wanting to set up the theme for a while so that if I didn’t have the time (or inspiration) to create a custom image for lead_image and secondary_image, the page would revert to using a default one. Plus, of course, it would mean I could occasionally post remotely from places where I didn’t have access to ftp and image software. Turns out it’s not actually that hard.

The line of code that looks for the image to use on your home page is, funnily enough, home.php. (There’s a similar entry under index.php but it only looks for secondary images, so I suspect that’s called when you’ve got some kind of page navigation plugin that lets you go back to previous entries. I put the fix in there too just to be certain.)

You’re looking for the code:

<?php $top_query = new WP_Query(‘showposts=1’); ?>
<?php while($top_query->have_posts()) : $top_query->the_post(); $first_post = $post->ID; ?>
<div class=”post” id=”post-<?php the_ID(); ?>”>
<div class=”main-post-bg”>
<p class=”post-comments”><?php comments_popup_link(‘No Comments’, ‘1 Comment’, ‘% Comments’); ?></p>
<?php $image = get_post_meta($post->ID, ‘lead_image’, true); ?>
<a href=”<?php the_permalink() ?>” title=”<?php the_title(); ?>”><img width=”593″ src=”<?php echo $image; ?>” alt=”” /></a>
<div id=”post-<?php the_ID(); ?>” class=”post”>
<div class=”main-post-bg”><a title=”<?php the_title(); ?>” href=”<?php the_permalink() ?>”><img src=”<?php echo $image; ?>” alt=”” width=”593″ /></a>

The line in there that calls the image is <?php $image = get_post_meta($post->ID, ‘lead_image’, true); ?>.  You can add in a string just after the last semicolon that tells it what to look for if there is no ‘lead_image’ specified by changing it to:

<?php $image = get_post_meta($post->ID, ‘lead_image’, true); $image = $image ? $image : ‘../images/default_img.jpg’; ?>

Of course, you’d change the path to whatever points to your default picture, and make sure it’s sized properly (593 x 225px).

Then you need to do something similar to point to your default ‘secondary_image’.  It looks just like the above line, but about 15 lines down and says ‘secondary_image’ instead of ‘lead_image’.  That one will need to be sized 293×150, if you’re still using the unstandard default sizing.  To make it easy, here’s a cut and paste again:

<?php $image = get_post_meta($post->ID, ‘secondary_image’, true); $image = $image ? $image : ‘../images/default_img2.jpg’; ?>

And finally, for those that have asked me how I made my home page show only selected posts, I created a category called ‘Featured’ and only those will show up on the front page.  It was my workaround for posts where I didn’t have images – I just wouldn’t assign them to that category and the pictures wouldn’t be needed.  How?  The line:

<?php $top_query = new WP_Query(‘showposts=1’); ?>
…and further down…
<?php query_posts(‘showposts=15’); ?>

got changed to:

<?php $top_query = new WP_Query(‘showposts=1&category_name=Featured’); ?>
<?php query_posts(‘showposts=15&category_name=Featured’); ?>

Technorati Tags: , , , , ,

Leave your own comments