David Allen
David Allen

Reputation: 1163

custom post page template

I'm having a problem with this script which should load a custom template when view a custom post page.

I've place a echo command to make sure that the url is correct but i doesn't even echo the url.

function da_custom_post_type_template($single_template) {
     global $post;

     if ($post->post_type == 'include') {

          $single_template = PLUGIN_PATH. 'template/custom_template.php';
     }
     echo $single_template;
     return $single_template;
}
add_filter( "single_template", "da_custom_post_type_template" ) ;

Please help

Upvotes: 0

Views: 239

Answers (3)

David Allen
David Allen

Reputation: 1163

Finally done it....

I re downloaded wordpress and change my code to below...

gobal $post didn't seem to have anything set so i used $wp_query instead. Thanks CroiOS for pointing that out.

And also thanks to CMF for point me into the right direction with my installation on word press issue. i installed WP via SSH so i'm wondering if I downloaded a nightly build or something like that..

function da_custom_post_type_template( $template ) {
    global $wp_query;
    if ($wp_query->post->post_type == 'include') {
      $template = PLUGIN_PATH . '/template/custom_template.php';
    }
    return $template ;
}
add_filter( "archive_template", "da_custom_post_type_template" ) ;

Also for some reason its class as a archive_template :)

Upvotes: 0

AriePutranto
AriePutranto

Reputation: 401

You can still filter single template, actually. It still exists on wp-includes/template.php.

I can't see nothing wrong on your function. Are you sure that the template file is exists?

Edit:

Try this:

function da_custom_post_type_template( $template ) {
    global $post;

    if ($post->post_type == 'include') {
        $template = dirnamr( __FILE__ ) . '/template/custom_template.php';
    }
    return locate_template( $template );
}
add_filter( "single_template", "da_custom_post_type_template" ) ;

Upvotes: 1

iWizard
iWizard

Reputation: 7104

Put this into single.php:

<?php
      global $wp_query;

      $post = $wp_query->post;
      $post_type = get_query_var('post_type');

      if($post_type == 'include'){
        include(TEMPLATEPATH.'/my_post_template.php');
      }else{
        include(TEMPLATEPATH.'/default_post_template.php');
      }#end else

?>

Upvotes: 1

Related Questions