WordPress Template Setup

Posted: January 4, 2012 in Wordpress
Tags:

Here is the first part of a tutorial on how to develop WordPress templates, it explains template hierarchy, useful settings and the data structure.

To access other parts of the tutorial see the WordPress Template Tutorial post.

Template Hierarchy

The first concept to learn is the hierarchy of the files that WordPress uses to build the template. WordPress automatically uses the files on the “/wp-content/themes/theme_name” folder depending on the url requested by the user.

So for example if the user is requesting the index page WordPress shows the file index.php, if instead the user is requesting a direct post page WordPress shows the file single.php, going on you have archive.php for multiple posts requests, page.php for pages requests, etc.. You can see all the cases of how WordPress chooses the page to be shown.

There is an advanced way, which I use, to organize the template hierarchy. You have to modify the functions.php file and add this code:

add_filter('category_template', 'my_category_template');
function my_category_template( $template ) {
  foreach( (array) get_the_category() as $cat ) {
    if ( file_exists(TEMPLATEPATH . "/category-".$cat->term_id.".php") )
      return TEMPLATEPATH . "/category-".$cat->term_id.".php";
  }
}
add_filter('single_template', 'my_single_template');
function my_single_template( $template ) {
  foreach( (array) get_the_category() as $cat ) {
    if ( file_exists(TEMPLATEPATH . "/single-".$cat->term_id.".php") )
      return TEMPLATEPATH . "/single-".$cat->term_id.".php";
  }
}

If you are using the language plugin WPML use this below instead:

add_filter('category_template', 'my_category_template');
function my_category_template( $template ) {
  foreach( (array) get_the_category() as $cat ) {
    if ( file_exists(TEMPLATEPATH . "/category-".icl_object_id($cat->term_id, "category", false, "en").".php") )
    return TEMPLATEPATH . "/category-".icl_object_id($cat->term_id, "category", false, "en").".php";
  }
}
add_filter('single_template', 'my_single_template');
function my_single_template( $template ) {
  foreach( (array) get_the_category() as $cat ) {
    if ( file_exists(TEMPLATEPATH . "/single-".icl_object_id($cat->term_id, "category", false, "en").".php") )
    return TEMPLATEPATH . "/single-".icl_object_id($cat->term_id, "category", false, "en").".php";
  }
}

With this code you have to make a category-x.php and a single-x.php file (where x is the number of category ID) for each category you want to make a custom page template. For example category-4.php is used to show http://www.minimit.com/category/works and all subs works categories, and single-4.php is used to show http://www.minimit.com/works/teknox-2011 and all others works pages.

As you can see this way you have great flexibility, and you can use the default template hierarchy for some categories if you still want to.

Useful Settings

The first setting you want to change after an install is the custom permalinks setting: by default WordPress uses permalinks with “?p=x” (where x is the ID of the category or of the post), which isn’t the best way to organize your links, because they have no content meanings, and you lose one of the most important search engine optimization tactic which is having keywords on the webpage’s url.

So go inside Settings>Permalinks and choose “Custom Structure” and add:

/%category%/%postname%/

Depending on the data you want to show in the url address you can use this codes instead:

%year% %monthnum% %day% %hour% %minute% %second% %post_id% %postname% %category% %tag% %author%


The second setting you want to change is the one that chooses how many post per page WordPress shows at most. Go inside Settings>Reading and change the “Blog pages show at most” value. If for example you don’t want to implement pagination use a very high number.



Another useful setting is to change the length of the excerpt, to do that you have to modify the functions.php file and add this code:

add_filter('excerpt_length', 'new_excerpt_length');
function new_excerpt_length($length) {
	return 70;
}

Replace 70 with the number of characters you want the excerpt to be long.

The Data Structure

Let’s dive into how WordPress stores data and how to setup our data structure.

The first thing you want to do is to install a multilanguage plugin if you are using more than one language, I recommend WPML Multilingual.

Then put the categories you want under Post>Categories; put them with the order you want them to appear on the website menu.



Now when you add a new post with Post>Add New you can select the categories the post has, so that your WordPress template will build the pages differently using a custom page template for each category, as seen before.

Every time you make a new post now you have to add the custom fields manually, which isn’t optimal for client usage. You can have a much better usability, with the custom fields that you need visible and ready to be filled.

There are some methods to achieve that, I valuated Custom Post Fields and other options, and the simplest method is to use the Magic Fields plugin, because it implements the categories, because it fully support WPML and because with it is very easy to add custom meta boxes with it.



You just have to install Magic Fields, create and assign a write panels to categories, and then add the custom fields you need for each panels.

Courtesy

Source:-http://www.minimit.com/articles/wordpress-template-setup

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s