Useful WordPress Code Snippets

Here’s a growing collection of code snippets I find useful.

Enable the Post Thumbnail Feature

Post thumbnails were added in WordPress 2.9. Justin Tadlock has a great article about this. To enable the feature in your theme, simply add this code snippet to the functions.php file:

if ( function_exists( 'add_theme_support' ) ) {
	add_theme_support( 'post-thumbnails' );
}

To Use Post Thumbnails

<?php the_post_thumbnail( 'thumbnail' ); ?>
<?php the_post_thumbnail( 'medium' ); ?>
<?php the_post_thumbnail( 'full' ); ?>

Get the Post Name

This function is great if you need to style a certain element differently from page to page.

<div class="<?php echo $post->post_name; ?>">
</div>

About Devin

I'm a WordPress developer based in Austin, Texas. Follow my projects on GitHub, or more general WordPress ramblings as @devinsays on twitter.

One comment on “Useful WordPress Code Snippets

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>