WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Custom post type single page template

[Resolved] Custom post type single page template

  • Hello guys!

    So I registered a custom post type “products”.
    I have my single-products.php to template the single posts.

    But I want to make an individual template for one of those. Let’s say the post with id=90

    How can I do it?

    Its something like page-90.php but more like post-90.php

    Any ideas?

Viewing 9 replies - 1 through 9 (of 9 total)
  • I tried adding the page-attributes support but the template option isn’t available

    Gyrate360
    Member

    @57842312-1

    add_filter('single_template', 'my_single_template');
    function my_single_template($single) {
    	if(file_exists(get_template_directory() . '/single-' . get_the_ID() . '.php'))
    		return get_template_directory() . '/single-' . get_the_ID() . '.php';
    	return $single;
    }

    Then just create a file single-90.php,that’s ok!

    That worked perfect man!!!!

    I’m very thankful!

    Gyrate360
    Member

    @57842312-1

    nice,can we make friends,my email is [email redacted – please keep help/discussion on these forums]

    WPyogi
    Forum Moderator

    @wpyogi

    Please keep help/discussion on these forums –

    http://codex.wordpress.org/Forum_Welcome#Helping_Out

    @Gyrate360 – the website in your user profile appears to be violating WordPress’s domain name/trademark policy –

    http://wordpress.org/about/domains/

    exedesign2
    Member

    @exedesign2

    I used the function above to use a custom single post, but no success.
    I used a custom post type for my store so the pages won’t be mixed with regular site pages.
    I have example.com/store/checkout
    and I used single-842.php based on the page checkout id.
    Still not displaying checkout page based on single-842.php
    I am using wordpress 3.8

    keesiemeijer
    moderator

    @keesiemeijer

    Have you tried it with a single.php template for your custom post type (single-store.php if store is the custom post type name).
    http://codex.wordpress.org/Template_Hierarchy#Single_Post_display

    Here is another way to get a template based on the post id:

    add_filter( 'template_include', 'single_id_template', 99 );
    
    function single_id_template( $template ) {
    
    	$post_id = get_the_ID();
    
    	if ( is_single() &&  $post_id ) {
    		$_template = locate_template( array( 'single-' . $post_id .'.php'  ) );
    		$template = ( $_template ) ? $_template : $template;
    	}
    
    	return $template;
    }

    exedesign2
    Member

    @exedesign2

    Thanks, the above functions works great. I have a solution, but it was too lengthy. I added the template page option to the custom post type, but it’s ended up been on other pages.

    keesiemeijer, THANKS a lot for last snippet, it saved a lot of time to me =)

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] Custom post type single page template’ is closed to new replies.