WordPress.org

Ready to get started?Download WordPress

Ideas

Template files for single post-ID

  1. wonderyak
    Member

    I've recently come into a couple situations where having a default post template by ID would come in VERY HANDY.

    Since pages already get this treatment {page-ID.php} it would seem a logical extension for posts.

    If this is too convoluted, just a plain ID template for posts/pages/what-have-you would be awesome.

    Posted: 2 years ago #
  2. Ipstenu (Mika Epstein)
    Half-Elf Support Rogue & Mod

    page-ids are far fewer in number (for most people)... Posts are more numerous.

    There already is a default post template (single.php or single-post.php). If you need custom posts, you're supposed to use custom post-types.

    Check out http://codex.wordpress.org/Template_Hierarchy

    Posted: 2 years ago #
  3. Azuaron
    Member

    12345

    If you have a single post that you want to be display in a specific way, it sounds like you have a page, or, at most, a custom post type.

    Posted: 2 years ago #
  4. wonderyak
    Member

    I understand the template hierarchy well; there is no way to set a specific template for a single post however. I feel the API is lacking in this regard.

    Posted: 2 years ago #
  5. Ipstenu (Mika Epstein)
    Half-Elf Support Rogue & Mod

    Azuaron's right, you use CPTs for that :)

    Posted: 2 years ago #
  6. shazdeh
    Member

    The Hybrid framework has a more advanced template hierarchy, one is the post-ID.php file. if you still need it, the function required for this is in functions/template-hierarchy.php file: http://themehybrid.com/hybrid-core

    Posted: 2 years ago #
  7. webaware
    Member

    12345

    Easy enough to accomplish with a few lines of code in your single.php:

    global $post;
    if ($post && $post->ID = 123) {
        include TEMPLATEPATH . '/single-123.php';
        exit();
    }
    Posted: 1 year ago #
  8. Ipstenu (Mika Epstein)
    Half-Elf Support Rogue & Mod

  9. Maor Chasen
    Member

    This piece of code will do the trick. Place it in your functions.php or functionality plugin.

    <?php
    
    function my_single_template_by_post_id( $located_template ) {
    	return locate_template( array( sprintf( "single-%d.php", absint( get_the_ID() ) ), $located_template ) );
    }
    add_filter( 'single_template', 'my_single_template_by_post_id' );

    Gist: https://gist.github.com/4184748

    BTW, I don't think this should ever be implemented into core. There are a handful of filters out there that can help you target the right template file of your choice (the majority of WordPress users will simply never use this feature). And it hurts how easy it is to implement.

    Posted: 1 year ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    5 Votes
  • Status

    This idea is under consideration