WordPress single post templates withouth any plugin needed

November 16th, 2008

Until few days ago I was blabbering around with my friends how I dislike the WordPress due to lack of object oriented programming features and so on. This weekend I revised my thoughts and come to conclusion, once again, how WordPress is great. Sometimes OOP is not the best way to go. And truth be told, OOP CMS solution’s are sometimes far more slower. This weekend I got a whole new look at WordPress and the way of using it. I’ll be working on a project, a community site for my friend and decided to go with WordPress.

This will not be the regular WordPress posts and pages. I plan to implement a whole new philosophy into it. I’ll be using an outside /mylibrary folder with full object oriented classes and methods representing models which I’ll be working on. I wrote few lines of code inside the index.php file in the root of WordPress folder to autoload all of my classes and stuff from /mylibrary when needed to avoid include’s ore require’s inside each of my template files.

Most of the stuff inside /mylibrary will call the default built in WordPress functions for manipulating posts and pages, therefore most of /mylibrary stuff will work only inside theLoop of the WordPress template (for now). My idea is to use the Custom Fields functionality, introduced in the newer versions of WordPress, extensively. My idea is to have a special category, let’s say, “user” and a post assigned to each user like “first-lastname” then the page generated by the url “http://somesite.domain/user/first-lastname” with the basic post data plus the custom field data.

All of this data assigned to the “user” post inside the “/user” category will be done transparently to the user. I already made a class that automatically creates a post with basic post daa like title. content, excerpt plus data like tags and custom fields. All I need to do now is to create a page with some form on it and tie my form submit action to execute that class and method I created passing it all the necesery data. This may sound like a lot of work, or even complicated, but hey… few weeks with Magento and WordPress becomes a toy :)

Anyhow… there are still few things I’m looking around to get the full picture and power of WordPress. One of them is Single post custom template. If you open admin interface in WordPress and go to Edit > Page you can see that you can assign a template foreach page if you like. So my question is, why can’t you do it for Single post? Well, you can, just keep on reading :)

My idea is to create directory /layouts inside the root of my custom theme. Inside directory /layouts I created two additional folders named /category and /post. Inside directory /category I would like to have special template file foreach of my categories. If my url is like

http://somename.domain/categoryname/postname

then opening http://somename.domain/categoryname/ would open the category template, which by default is archive.php file and I want it to open the /layouts/category/categoryname.php file.

If I were to open the
http://somename.domain/categoryname/postname
I would be directed to single.php file by default and I want it to be pointing to /layouts/post/categoryname.php template file.

So how do I get the defaults to do what I mentioned previously.

All you need to do is to open the functions.php file and place the following code in it. I placed it at the very top of the functions.php file.

<?php

/**
* Loads the single post template
* I have set the /layouts subfolder as the folder to store some custom layouts
* Study the function body… this function ca be reused to fully manage layouts
* in various scenarios
*
* Currently function is written to load the /layouts/post/catName.php file
* instead of the single.php file. So if you created the category “stuff”
* and added few templates to it, then when you open the post for that category
* this function fill try to load the post in yourtheme/layouts/post/stuff.php file
*
* @author Branko Ajzele
*/
add_filter(’single_template’, create_function(’$t’, ‘foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . “/layouts/post/”.$cat->name.”.php”)) return TEMPLATEPATH . “/layouts/post/”.$cat->name.”.php”; } return $t;’ ));

/*

in the statement
foreach( (array) get_the_category() as $cat )

$cat is a object of type stdClass as below

stdClass Object
(
[term_id] => 4
[name] => duznosnik
[slug] => duznosnik
[term_group] => 0
[term_taxonomy_id] => 4
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 3
[object_id] => 16
[cat_ID] => 4
[category_count] => 3
[category_description] =>
[cat_name] => duznosnik
[category_nicename] => duznosnik
[category_parent] => 0
)

*/

add_filter(’category_template’, create_function(’$t’, ‘foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . “/layouts/category/”.$cat->name.”.php”)) return TEMPLATEPATH . “/layouts/category/”.$cat->name.”.php”; } return $t;’ ));

?>

Hope some of you find this useful. You can play around with the add_filter function to make more complex layout routes, just be careful not to get lost :)

Space Art 101

November 16th, 2008
Category: Photoshop > Effects

How to create realistic planets in Photoshop Cs3 or higher.

Post date: Nov 15, 2008

Interested in free project management tool

November 15th, 2008

Recently I discovered dotProject. Not so cool looking as much as cool working application written in PHP for project managment. Have a look at this article.

If you experience problems with PHP errors at Files menu after installing the application here is what you might do to fix it.

Open the /modules/files/index_table.php file and somwere around line 105 you will find the code that’s causing the error. Just replace it like

/*
ORIGINAL CODE

$r->addQuery(’DISTINCT count(f.file_id) as file_versions’
. ‘, max(DISTINCT f.file_version) as file_lastversion’
. ‘, f.file_version_id, f.file_project’);

*/

/* MODIFIED CODE */
$r->addQuery(’DISTINCT count(f.file_id) as file_versions’
. ‘, max(f.file_version) as file_lastversion’
. ‘, f.file_version_id, f.file_project’);

Do the same replacement for folders_table.php file around line 280.

After that, your Files tab should work.

One notice, i experienced this problem on my live hosting, not on my local machine.

Merry Christmas tree design

November 15th, 2008
Category: Photoshop > Design

This tutorial teaches you how to create a stylized Christmas tree design with stars

Post date: Nov 13, 2008

How do you import videos into PPT for FREE

November 15th, 2008
Category: Flash > Animation

Now I'd like to tell you how to make the slideshow with videos from YouTube, MySpace, Metacafe, etc. with freeware.

Post date: Nov 13, 2008

How do you combine several FLV files into one for free

November 15th, 2008
Category: Flash > Animation

This tutorial is to tell you the easy way to combine several FLV files to a single one for free, so that you neednt watch videos with the annoying breaks.

Post date: Nov 13, 2008

How to Create Real Estate Flash Photo Gallery Presentation

November 15th, 2008
Category: Flash > Animation

This tutorial will show you how to create real estate flash photo gallery presentation step by step.

Post date: Nov 13, 2008

Useful Applications for the Clone Stamp Tool

November 15th, 2008
Category: Photoshop > Basics

Useful Applications for the Clone Stamp Tool

Post date: Nov 14, 2008

Design a Shiny Bass Guitar Illustration Using Photoshop

November 15th, 2008
Category: Photoshop > Drawing

Design a Shiny Bass Guitar Illustration Using Photoshop

Post date: Nov 14, 2008

How to Create a Magic Wand Icon

November 15th, 2008
Category: Illustrator > Drawing

How to Create a Magic Wand Icon

Post date: Nov 14, 2008