The Blog

How to manage custom post type category template on wordpress 3.0

18 Giu 10

Today wordpress 3.0 has been released. Since i started to play with it almost immediately i found some troubles when i was trying to display all the post in the new wordpress custom type into one single page.

The goals:

  1. I wanted to create a custom template for these custom posts.
  2. I wanted to create a custom archive template for these custom posts

I found immediately the solution for the first “issue” . WordPress 3.0 templating system would search for a file named single-[custom post type name].php inside the theme directory so i simply copied and pasted the single.php into the new file and made some tiny modifications.

The second goal was a little bit difficult to achieve. I googled it and i found some articles talking about this issue but none of them gave Β ma an easy and working solution.

So i decided to take some parts from them and make my own solution.

Note: The custom post type name is “androidapps”

I did create a new useless plugin that fixed my issues. the source coude is the following:

[sourcecode language=”php”]/*
Plugin Name: Androidapss
Plugin URI: http://i love you.com
Version: 1.0.78
Description: A very useless plugin
Author: Baccega Andrea
Author URI: http://www.andreabaccega.com
Tags: seo,google,link,optimization
*/
add_filter(‘rewrite_rules_array’,’wp_insertMyRewriteRules’);
add_filter(‘query_vars’,’wp_insertMyRewriteQueryVars’);
add_filter(‘init’,’flushRules’);
add_action("template_redirect",’buch_template_redirect’);
// Remember to flush_rules() when adding rules
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
$newrules = array();
$newrules[‘androidapps/feed/(feed|rdf|rss|rss2|atom)/?$’] = ‘index.php?&feed=$matches[1]&post_type=androidapps’;
$newrules[‘androidapps/(feed|rdf|rss|rss2|atom)/?$’] = ‘index.php?&feed=$matches[1]&post_type=androidapps’;
$newrules[‘androidapps/page/?([0-9]{1,})/?$’] = ‘index.php?&paged=$matches[1]&post_type=androidapps’;
$newrules[‘androidapps/?$’] = ‘index.php?paged=1&post_type=androidapps’;
return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
array_push($vars, ‘id’);
return $vars;
}

function buch_template_redirect()
{
global $wp;

$muley_custom_types = array("androidapps");

if (in_array($wp->query_vars["post_type"], $muley_custom_types))
{
if ( is_robots() ) :
do_action(‘do_robots’);
return;
elseif ( is_feed() ) :
do_feed();
return;
elseif ( is_trackback() ) :
include( ABSPATH . ‘wp-trackback.php’ );
return;
elseif($wp->query_vars["name"]):
include(TEMPLATEPATH . "/single-".$wp->query_vars["post_type"].".php");
die();
else:
include(TEMPLATEPATH . "/".$wp->query_vars["post_type"].".php");
die();
endif;

}
}[/sourcecode]

Well all the trick is done by two functions

  • buch_template_redirect
  • wp_insertMyRewriteRules

The first one buch_template_redirect would hack the template functions and would tell wordpress to search for

  • single-androidapps.php for single posts
  • androidapps.php for archive

The second wp_insertMyRewriteRules which is copied from the wordpress codex website would add some rewrite rules to wordpress so he will knows how to route the requests :).

Now if you navigate through http://www.andreabaccega.com/androidapps you’ll see the archive template dedicated to the posts inside the “androidapps” type.

How would you use my code for your meanings?

Simply create a new plugin with the code above, then replace all those androidapps instances with your custom post type name.

Enjoy πŸ™‚

Comments

  • Hi and thanks for the post. I found it really helpful. Cool web page incidentally

  • Anna

    THANKS A LOT!!! Perfect solution!