Sage 9 - index.blade.php include partial with get_post_type()?

Why is it in index.blade.php and single.blade.php when using the @include directive for partials the get_post_type() function is used? The other views files don’t use it.

ex: @include(‘partials.content-’.get_post_type())

Any one know the reason for this?
When making a home.blade.php should I be using the get_post_type() when including partials?

To include partials specific to post types. i.e if it’s called in the loop of a Post, then the include will resolve get_post_type() to @include('partials.content-post').

Depends on which partials you want to include.

Basically


@include('partials.content-page')

inserts the rendered content of the file, ./resources/views/partials/content-page.blade.php, where the @include appears.

get_post_type() is acting like a variable here to complete the string “partials.content-_______”.

So, to answer your question about home.blade.php (I use front-page.blade.php instead… you can read the lengthy WordPress Documentation on Template Hierarchy for a good time): Try not using get_post_type() — if things break, put it back. If not, leave it deleted.

I understand that get_post_type() is acting as a variable to complete “partials.content-_______” .
But why would I need the post type at the end a partials file name? They are partials of template files and not the base template files.

When using @include(‘partials.content-’.get_post_type()) it renders content.blade.php.
Why does it disregard the hyphen - ? I’m going to assume it gets “post” and then it doesn’t find content.blade-post.php so it renders content.blade.php.

And to my understanding out of the partial content files only “page” is a post type.

I have removed get_post_type() from my home.blade.php from the 2 of my partial calls and it still works.

In the event that partials.content-{CUSTOM POST TYPE} doesn’t exist, it will fall back to the default partials.content. This is why your template still works after removing get_post_type().

The design philosophy around this is to allow you to rely on the default partials.content template throughout your website and only create a different template for different post types if you need to, allowing your code to remain DRY (do not repeat yourself).

A great example would be the different between a loop of blog posts and a loop of team members. Both need different content in a lot of designs so you would deviate from the default template to facilitate this.

This topic was automatically closed after 42 days. New replies are no longer allowed.