Custom Templating System

This is not entirely bad code, rather more an example of doing something that is really unnecessary. The developer of this snippet wanted a way to create custom template files based on the post type that was displayed.

<?php get_header(); ?>

<?php
$post_type=$_GET['post_type'];
?>
<?php
if((get_query_var('property')) or ($post_type=='property')) {
        include "single-house.php";
} else {
        include "single-news.php";
}
?>

<?php get_footer(); ?>

This has made the cut for Crappy Code because it is completely unnecessary. WordPress has a built-in template system that is extremely powerful and includes native support for post-type-specific template files. All the developer needed to do here was create a file in the theme folder called single-{post type name}.php. If the post type name is “property”, the file name would be single-property.php.

Submitted by Lars Koudal.

Advertisements

8 thoughts on “Custom Templating System

  1. Wouldn’t this method though keep the person from having to copy the file into the theme folder? When you are building things and trying to make them as easy as possible for the non-tech savvy person who clicked on “Install Themes” and has no idea how to move/copy the files. Seems like a solution like this isn’t a bad idea. I think the code could potentially be improved by using is_singular (‘property’) as the check…

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