In: General
16 Nov 2008Until 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
1 Response to WordPress single post templates withouth any plugin needed
DN
December 10th, 2008 at 4:29 am
I’m getting this error.
Parse error: syntax error, unexpected T_VARIABLE in /home/dn0922/public_html/wp-content/themes/theme/functions.php on line 169
that line is…
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;’ ));
I’m a total newbie with PHP so I have no idea how to fix it…