2

Hi I have question about file names. If I have custom post type, and for template I can create for example single-portfolio.php and get content of portfolio posts into this file, but i need to get that post not in same file I want get it on individual files for example single-portfolio-post1.php something like this but second file that I create doesn't work how can i do that?

Thank you

1

You can use the dynamic filter {type}_template (where {type} is the current query type e.g. single, index etc.) found in get_query_template():

function wpse_204848_get_id_template( $template ) {
    if ( $post = get_queried_object() ) {
        if ( $_template = locate_template( "single-{$post->post_type}-{$post->ID}.php" ) )
            $template = $_template; 
    }

    return $template;
}

add_filter( 'single_template', 'wpse_204848_get_id_template' );

For a portfolio post of ID 5, it will use the template single-portfolio-5.php if it exists.

1

As of WordPress version 4.4, this kind of template structure:

single-{post_type}-{post_name}.php 

will be supported. More here on make.wordpress.org.

1

WordPress 4.3 and below

There's nothing in the template hierarchy for showing a template based on a post ID unless you're using the page post type.

Instead you're going to have to place conditional logic at the top of your single-portfolio.php.

I would also recommend you use meta options, terms, or the post slug instead of the ID.If you use post IDs, a simple import and export of the content would break your theme.

Also consider implementing a metabox with a dropdown that allows you to choose a template. This way you're not restricting yourself and hardcoding content in your theme, and you can build reusable general templates for certain portfolio items

WordPress 4.4

4.4 adds a new template to the hierarchy that takes the form:

single-{post_type}-{post_name}.php

A new theme template has been added to the theme hierarchy as of r34800: single-{post_type}-{post_name}.php. This template follows the rules of is_single() and is used for a single post or custom post type. It’s most useful for targeting a specific post in a custom post type, and brings consistency to the templates available for pages and taxonomies. It comes in the hierarchy before single.php and single-{post_type}.php.

Don’t forget, too, that in WordPress 4.3 singular.php was introduced and the hierarchy of attachment templates was fixed.

0

A little different answer then the others

If you have a different design for a specific post it is better to try to see what can you do with CSS only before resorting to writing a new template.

Still the question no is how do you identify which content should go with design A and which with B, for which I think the best thing to do is to add a meta box in which a design can be seleceted and adopt your sing-{post type}.php code to take it into consideration. If it is very different then you can put the code in different file, but the important point here is giving the admin control over the template being used.

The reason for giving control to the admin is that you might want to reuse the template, and things like IDs and slugs can change (especially slugs)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.