11 2

How to force child categories to use the PARENT category template in Wordpress

After much digging around and trying to find a suitable plugin or PHP if/then statement, I came across a nice little site that did it for me using the functions.php page.

I would love to say I wrote this handy bit of code myself but I did not. Much praise to for the skilled coder at brassblogs.

Now, the way to use this is to take the code listed below and add it to your functions file. Or, to make it easier, I’ve pasted the complete information into this text file.

function inherit_template() {
if (is_category()) {
$catid = get_query_var('cat');
if ( file_exists(TEMPLATEPATH .
'/category-' . $catid . '.php') ) {
include( TEMPLATEPATH . '/category-' .
$catid . '.php');
exit;
}

$cat = &get_category($catid);
$parent = $cat->category_parent;
while ($parent) {
$cat = &get_category($parent);
if ( file_exists(TEMPLATEPATH
. '/category-' . $cat->cat_ID . '.php') ) {
include (TEMPLATEPATH
. '/category-' . $cat->cat_ID . '.php');
exit;
}
$parent = $cat->category_parent;
}
}
}

add_action('template_redirect',
'inherit_template', 1);

After you add that to your functions.php file, you simply need to make a template for EACH PARENT CATEGORY. The child categories will then use the template that their PARENT uses. YEA!

It’s very easy to make a category template. The simplest way is to take the existing archive.php file in your Wordpress theme and save it as category-ID.php where the ID is the NUMBER of the category. You can check this by going into your wp-admin, clicking on the category and reading the complete URL for the ID= (it will be at the end of the string).

So, if your category is #1, you will make category-1.php. You can style this page in any way you want; you can change link colors, text colors or even make it look completely different from the rest of your Wordpress blog – it’s completely up to you!

This will apply to all parent categories that you want to use a special template; simply find the category ID, create the page, name it category-XX.php (where XX is your category ID) and upload it to your theme folder. Or use the sample you can download here.

Again, credit goes to brassblogs.com! Thanks!



9 20

Display recent comments on your Wordpress blog

Sometimes you want to display a Wordpress feature, such as recent comments, tags, etc., without using a widget. For me, it’s occassionally easier to STYLE, especially if I’m not using a Theme that I built or that I’m familiar with.

A great code around is shown below, courtesy of to Kyle Eslick and found on wprecipes. I’ve used it on some sites and I find it works great. ONE NOTE: if you want less than 10 comments, simply change the code to a lesser number.

php
  global $wpdb;
  $sql = "SELECT DISTINCT ID, post_title, post_password,
comment_ID, comment_post_ID, comment_author,comment_date_gmt,comment_approved,
comment_type,comment_author_url,
SUBSTRING(comment_content,1,30) AS com_excerpt
FROM
$wpdb->comments LEFT OUTER JOIN $wpdb->posts ON
 ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
 WHERE comment_approved = '1' AND comment_type = ''
AND post_password = ''
ORDER BY comment_date_gmt DESC LIMIT 10";

  $comments = $wpdb->get_results($sql);
  $output = $pre_HTML;
  $output .= "\n


";
  $output .= $post_HTML;
  echo $output;



7 28

Adding thumbnail support to your Wordpress theme

Yea Wordpress! Starting in version 2.9, thumbnail support is built into the code, which means that instead of doing all the hard work, Wordpress has done it for you! For the most part anyway…

It’s a pretty simple process.

1. Add a line of code to functions.php

add_theme_support('post-thumbnails');

2. Define the size of the thumbnails – you have a couple options. You can hard crop it, which is what I have done in the example below (add ‘true’ to the end of the dimensions), or you can resize the box. Box resizing shrinks an image without distortion until it fits inside the “box” you’ve specified with your dimensions.

set_post_thumbnail_size( 100, 100, true ); // 100 pixels wide by 100 pixels tall, hard crop mode

Together this will look like:

3. Add the code to your template

That’s it! Now when you login to your wp-admin panel scroll down – on the right side under ‘Categories’:

Click on ’set thumbnail’ and you get this box:

Be sure to select ‘Use as thumbnail’ and then ’save’. Then close the window with the X on top right.

Here’s an example of it used on one of my sites: