23 responses to “Thesis Theme: How to Use Custom Templates With WordPress Custom Post Types”

  1. Rick Anderson

    This is a brilliant solution to a problem I’ve been working on for a while. In the past I’ve had to resort to using a post template plugin and then creating a template that would probably need revision in the next Thesis release.
    Your solution makes custom post templates upgrade proof. Way to go.

  2. Louisa

    Damn! I need to visit this site much more often!! I had to resort rearranging my entire site to using page templates rather than the much preferred post template!

    Thanks for this brilliant solution, Dimas!
    Louisa

  3. David Alexander

    Great post. Im trying to create a custom post template that is the same as the default so I can use that as a start point as I want to make minor adjustments I am not 100% sure on.. As theres no single where in the files could I cut this out to add to a custom template file? Cheers, keep up the great work. Stumbled!

  4. David Alexander

    Thanks Dimas. I will take a look into this. I have done some pretty cool templates in the past, all written in the custom_functions instead of referencing a separate file “I prefer your method”. See http://experiments.quixoticmedia.org not entirely complete but each magazine is pulled from the loop as a post, backgrounds styled separately by css controlling a post which belongs to a category. Categories are just colours on the inside. Pretty cool, but trying to create the basic layout is more challenging than that aha. Funny world ay. Wish my luck.

  5. David Alexander

    Hey again, I used your template code fine with my first post, but any posts after that default to the original template. Is this something that Thesis need to fix?

  6. David Alexander

    I have also tried using if is_singular but it still only being picked up by the first custom post type I publish.

    if ( is_singular(‘Property’) )

    &

    if ( is_singular(‘Property’) ) require_once TEMPLATEPATH . ‘/custom/property.php’;

    and this still works for the first post but not the rest. really odd.

  7. David Alexander

    By Thesis? or by me? If by me I have adjusted the both so they both get Property not location and the template file gets property.php or do you mean you need to call property.php twice?

  8. David Alexander

    Im using the following Dimas.

    // Template selection
    add_action('template_redirect','my_template_redirect');
    function my_template_redirect()
    {
    if (get_post_type() == 'Property')
    {
    global $wp_query;
    add_post_meta($wp_query->post->ID,'_wp_page_template','custom_template.php',TRUE);
    $wp_query->is_page = true;
    }
    }
    // custom template
    remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');
    add_action('thesis_hook_custom_template', 'my_thesis_hook_custom_template');
    function my_thesis_hook_custom_template() 
    { 
    if (get_post_type() == 'Property') require_once TEMPLATEPATH . '/custom/property.php'; 
    }
    
  9. David Alexander

    Okay, thanks Dimas, I shall try that tonight. That said I only have one custom post type so does that still apply? when I said it only affects my first post type what I meant is this. I created property1 and the template works, when I create property2 and 3 and 4 none of them pick up the template even though they are identical to property1. Really weird I thought. My template relies heavily on global to function so not sure if I would be able to proceed with the way I wanted.

    Examples see: http://housesforsaleinfrance.org/property/testproperty/

    then the second property I added: http://housesforsaleinfrance.org/property/testproperty2/

    Even though they are both the same post type only the first picks up the template.

  10. David Alexander

    Thanks Dimas, your far more informed than me. I think I will go and check phpmyadmin and see if I can see it in the db. Really confusing me at the moment though that it would work for one property post and not another. I spoke to Chris today and 1.8 wont have any new support for anything to do with custom post types from the sound of it, probably not coming until 2.0 so kinda in a muddle now. Really appreciate all your replies mate.

  11. Benjamin

    Dimas,

    Thank you very much for this post and the post on creating custom types- they are great.

    Could you give my code a once over and see if something sticks out to you? I created a Content Type called ‘recruit’, and created one entry. However, the redirect function does not seem to be firing.

    I am willing to buy ya a coffee for your guidance 😉

    add_action('init','my_init');
    function my_init() 
    {
    $labels = array
    (
    'name' => _x('Recruits', 'post type general name'),
    'singular_name' => _x('Recruit', 'post type singular name'),
    'add_new' => _x('Add New', 'recruit'),
    'add_new_item' => __('Add New Recruit'),
    'edit_item' => __('Edit Recruit'),
    'new_item' => __('New Recruit'),
    'view_item' => __('View Recruit'),
    'search_items' => __('Search Recruit'),
    'not_found' =>  __('No recruits found'),
    'not_found_in_trash' => __('No recruits found in Trash'), 
    'parent_item_colon' => ''
    );
    $support = array
    (
    'title',
    'editor',
    'author',
    'thumbnail',
    'excerpt',
    'custom-fields',
    'comments',
    'revisions',
    'page-attributes'
    );
    $args = array
    (
    'labels' => $labels,
    'public' => TRUE,
    'rewrite' => array('slug'=>'recruits'),
    'capability_type' => 'post',
    'hierarchical' => TRUE,
    'supports' => $support,
    'menu_position' => 21,
    'menu_icon' => '/assets/fork-knife.png'
    ); 
    register_post_type('recruit',$args);
    }
    function my_template_redirect()
    {
    if (get_post_type() == 'recruit')
    {
    global $wp_query;
    add_post_meta($wp_query->post->ID,'_wp_page_template','custom_template.php',TRUE);
    $wp_query->is_page = true;
    }
    }
    // custom template
    remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');
    add_action('thesis_hook_custom_template', 'my_thesis_hook_custom_template');
    function my_thesis_hook_custom_template() { ?>
    Test..!
    <?php
    }
    
  12. Benjamin

    Wow. That was it!

    Dimas, may/where may I paypal you enough to cover your next venti latte?

  13. ENEM Technologies Pvt Ltd

    Wow….

    Very Nice Post ….

  14. Loris

    Very useful, thanks! I have applied your code for one of my custom post type, but it looks like the new template is being used for the list of posts, rather than for the post itself. Am I missing something?

  15. François

    Hi Dimas,

    I’ve tried the code from Benjamin and added the my_template_redirect.

    It works well in the admin (I can create new posts, …)

    I can preview them but not see them if I publish them. I get the famous “You 404’d it. Gnarly, dude.”

    Any clue of what I am missing?

    Regards,

  16. François

    Oh I was missing the permalinks update.

    Thank you for your tutorial !!!

  17. John Tanedo

    Hi Dimas,

    The solution looks promising but I tried it on my website http://test3.thinkbigonline.com.au/testing-post/ but I had no luck customizing the single posts design…

    I placed these codes on custom_functions.php

    add_action('wp_head','add_ie_css');
    add_action('template_redirect','my_template_redirect');
    function my_template_redirect()
    {
    if (get_post_type() == 'location')
    {
    global $wp_query;
    add_post_meta($wp_query->post->ID,'_wp_page_template','custom_template.php',TRUE);
    $wp_query->is_page = true;
    }
    }

    // custom template
    remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');
    add_action('thesis_hook_custom_template', 'my_thesis_hook_custom_template');
    function my_thesis_hook_custom_template()
    {
    if (get_post_type() == 'location' ) require_once TEMPLATEPATH . '/custom/custom_single.php';
    }

    and created a custom_single.php file with these codes in it

    <img src="/custom/images/blogImg.png" alt="" />

    posted on:

    Recent News updates

    <a href="">

    <img src="/custom/images/news.jpg" alt="news" class="imgSidebar" />

    to make it short what I want is to make my single posts (http://test3.thinkbigonline.com.au/testing-post/) look like this(http://test3.thinkbigonline.com.au/news/) but instead of showing the list of posts inside the maincontent box on the left, it will show the full content of a post, the comments template and the previous/next post navi…

  18. odegraciajr

    Is there a way we can do this on custom taxonomy page? I used this code on my CPT but i has no luck on custom tax pages.

    Thanks for a very wonderful code!

Leave a Reply