WordPress, is the most used and preferred blogging platform on the web. As obeserved on WordPress.com, there are over 22 million WordPress publishers as of February 2010: 10.6 million blogs hosted on WordPress.com plus 11.4 million active installations of the WordPress.org software. So, bloggers using WordPress must know WordPress from Top to Deep in order to have a unique blog.
There are many sites which constantly publishes Free and Premium WordPress Theme like WooThemes, Elegant Themes etc. and what the most used coding in theme development is our new collection. Below are some most used hacks we gathered for making WordPress Themes.
Reduce Excerpt Length
WordPress, comes with a nice feature of adding <–more–> tag in the desired place. But if you want to manually select the words after which the more tag appears, then you can use the code.
function new_excerpt_length($length) { return 40;}add_filter('excerpt_length', 'new_excerpt_length');
The code is simple to understand, it returns 40, that is you can get your pagebreak after 40 words. You can change it according to you.
Turn Category ID into Name
This code just inputs a category ID and turns it into name.
/* Turn a category ID to a Name */ function cat_id_to_name($id) {foreach((array)(get_categories()) as $category) {if ($id == $category->cat_ID) { return $category->cat_name; break; }}}
Get Attached Image
If you want to fetch thumbnails from the Image attached to the post, then this code may help you.
/* Get image attachment (sizes: thumbnail, medium, full) */function get_thumbnail($postid=0, $size='full') { if ($postid<1) $postid = get_the_ID(); $thumb = get_post_meta($postid, "thumb", TRUE); // Declare the custom field for the image if ($thumb != null or $thumb != '') { echo $thumb; } elseif ($images = get_children(array( 'post_parent' => $postid, 'post_type' => 'attachment', 'numberposts' => '1', 'post_mime_type' => 'image', ))) foreach($images as $image) { $thumbnail=wp_get_attachment_image_src($image->ID, $size); ?>
This piece of code can be seen in many WordPress Theme, which allows you to have your post thumbnails from Custom Fields or Image Attached to the post.
Get Posts from Specific Category
The best way to enhance your homepage is to show posts from specific category. You can use the below code to get recently published posts from the desired category.
function homepage_display_category_posts(){ if (is_home() || is_front_page()) { ?> <div id="cat"> <h2>Recent From Tutorials</h2><br> <?php $my_query = new WP_Query('category_name=tutorials&showposts=5'); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?> <ul><li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li></u> <?php endwhile; ?> </div> <?php } }
You can configure the code by just changing the value of category_name by replacing tutorials by your category slug. You can also change the number of posts to show by changing the number from 5 to your desired one.
One Comment
Useful tips. I will try the Reduce Excerpt Length snippet now..