Hack: WordPress post thumbnail or TimThumb

I ran into a snag with one of my WordPress themes that uses TimThumb to automatically take an article image and display it on the homepage. What if I wanted to implement the_post_thumbnail(); (WordPress 2.9+ hack) and override my TimThumb image??

I was able to acheive this using some simple PHP in the index.php file of my WordPress Theme.

Heres how I did it.

  1. Logged into the WordPress backend, and under the ‘Appearance’ tab, click ‘Editor’.
  2. Opened functions.php
  3. Added  if ( function_exists( 'add_theme_support' ) )
    add_theme_support( 'post-thumbnails' );
  4. Opened index.php
  5. Found  <?php if($myscript == null || $myscript == '') { echo my_image_no_resize(200,200); } else { echo my_image(200,200); } ?>
  6. Came up with my own if else statement, and replace the above code with:
    <?php if ( has_post_thumbnail() ) {the_post_thumbnail();}
    elseif ($myscript == null || $myscript == '') { echo my_image_no_resize(200,200); } else { echo my_image(200,200); } ?>

Wordpress thumbnail sreenshotPretty simple hack that took me some time to figure out.

Here’s how it works…

We added the (step 2) if ( function_exists( 'add_theme_support' ) )   add_theme_support( 'post-thumbnails' ); so that it would enable us the option to add a thumnail image in our WordPress posts.

Once I could see the option in our posts, I opened the main index template file, index.php and found where the previous code that handles our timthumb insert takes place.

I then replaced it with an if/elseif statement that looks to see if there is we added a thumbnail via our post. If we didnt add one, timthumb kicks in as our backup.

Leave a Response

* Name, Email, Comment are Required