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

  1. Ariel Noname
    Posted 2 years 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
    Posted 2 years ago #

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

  3. Gyrate360
    Posted 2 years 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
    Posted 2 years ago #

    That worked perfect man!!!!

    I'm very thankful!

  5. Gyrate360
    Posted 2 years ago #

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

  6. WPyogi
    Forum Moderator
    Posted 2 years ago #

    Please keep help/discussion on these forums -


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


  7. exedesign2
    Posted 2 years 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
    Posted 2 years 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).

    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
    Posted 2 years 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.

  10. Alhana
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic