WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom post type single page template (9 posts)

  1. Ariel Noname
    Member
    Posted 9 months ago #

    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?

  2. Ariel Noname
    Member
    Posted 9 months ago #

    I tried adding the page-attributes support but the template option isn't available

  3. Gyrate360
    Member
    Posted 9 months ago #

    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!

  4. Ariel Noname
    Member
    Posted 9 months ago #

    That worked perfect man!!!!

    I'm very thankful!

  5. Gyrate360
    Member
    Posted 9 months ago #

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

  6. WPyogi
    Volunteer Moderator
    Posted 9 months ago #

    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/

  7. exedesign2
    Member
    Posted 6 months ago #

    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

  8. keesiemeijer
    moderator
    Posted 6 months ago #

    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;
    }
  9. exedesign2
    Member
    Posted 6 months ago #

    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.

Reply

You must log in to post.

About this Topic