7

I understand that when using get_template_part('content', get_post_format()); it will help choosing a post format for specific page according to post format.

And if there is no post format 'i.e it's standard', then it will fallback to content.php

But what if I used content-single.php with some logic like this:

if (get_post_format() == false) {
    get_template_part('content', 'single');
    } else {
        get_template_part('content', get_post_format());
}

Do I still need content.php page? is there any other functionalities that I don't know about?

3

No, content.php and content-single.php are not the same thing.

In your example CODE:

if (get_post_format() == false) {
    get_template_part('content', 'single');
} else {
    get_template_part('content', get_post_format());
}

WordPress will load content-single.php when get_post_format() is false. However, get_template_part( $slug, $name ) may still try to load content.php when you call it with get_template_part('content', get_post_format()); in the following example:

  1. get_post_format() returns (for example) video.

  2. But you don't have content-video.php template part file.

So basically, even when get_post_format() is not false, content.php will still provide you with the default template part if the corresponding post format related template part was not created.

Bottom line: whatever the main $slug is, it's always a good idea to keep the default template part file as the final fallback template part (in your case content.php is that default fallback template part file). So YES, you may still need it. So don't delete it, leave it alone.

The following is the part of the CODE from core function get_template_part. You'll see that, the core always loads $templates[] = "{$slug}.php"; as the final fallback template file:

function get_template_part( $slug, $name = null ) {
    // ... more CODE from WP core
    $templates = array();
    $name = (string) $name;
    if ( '' !== $name )
        $templates[] = "{$slug}-{$name}.php";

    $templates[] = "{$slug}.php"; 
    locate_template($templates, true, false);
}

Then, in locate_template function it loops through the $templates array until it finds the corresponding file, with the following CODE:

function locate_template($template_names, $load = false, $require_once = true ) {
    $located = '';
    foreach ( (array) $template_names as $template_name ) {
        if ( !$template_name )
            continue;
        if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
            $located = STYLESHEETPATH . '/' . $template_name;
            break;
        } elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
            $located = TEMPLATEPATH . '/' . $template_name;
            break;
        } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
            $located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
            break;
        }
    }
    if ( $load && '' != $located )
        load_template( $located, $require_once );

    return $located;
}

So as you can see from the above CODE, if you delete content.php, and if the theme user has a post format for which you don't have a template part file, WordPress will not find template file to fall back to, so will simply load nothing in that case. As a final attempt, WordPress tries to load a template part file from wp-includes/theme-compat/ core directory, but there is no content.php template part file in WP core.

Note: However, if you are building a child theme and the parent theme already contains the content.php file, then you don't need content.php file in the child theme (if you don't do any modification there), because in that case, WordPress will use the parent theme's content.php file as the fallback template part file.

0

You could get by with only the one template (content.php). That's usually less than ideal.

From the teamtreehouse's blog should clear your question.

For me that approach is a fallback way.

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.