I am currently creating a few templates for the various Blog formats, such as; 'Aside', 'Quote' and 'Gallery'.

I see a lot of people create such Templates by naming such files as:

  • content-aside.php
  • content-quote.php
  • content-gallery.php

Has anyone experienced any problems in renaming these, using a different format? For example:

  • format-aside.php
  • format-quote.php
  • format-gallery.php

Also, would there be a problem in placing these in sub categories or is it best practice to keep these files in the top level of the Theme Directory?

up vote 3 down vote accepted

There shouldnt be any issue, dont forget to change the slug in the template part call:

get_template_part( 'format', get_post_format() );

the best practice is to have them in their own folder:

get_template_part( 'template-parts/post/format', get_post_format() );

you can see the folder structure of twentyseventeen just change content respectively.

How get_template will work:

get_template_part will do a PHP require() for the first file that finds, it will create an array the first item array will use slug and name like this:

{$slug}-{$name}.php

the second item array will be:

{$slug}.php

the function will iterate the array of file names till it finds the one that exists.

so if you do a:

get_template_part( 'format', get_post_format() );

and for example the format is video the array of file names will be:

[0] "format-video.php"
[1] "format.php"

it will fallback to format.php, in the case of a Child theme the priority will be like this, with the video format as example:

[0] "format-video.php"  //in child theme
[1] "format-video.php"  //in parent theme
[2] "format.php" //in child theme
[3] "format.php" // in parent theme
  • I thought as much. I vaguely remember reading, some time ago, that 'content.php' file acted as a fall back file but couldn't find anything on Codex/WordPress Developers on the matter. Thanks for reaffirming my initial thoughts. – Craig Oct 14 '17 at 4:48
  • yeah it will, let me add info about it to the answer. – David Lee Oct 14 '17 at 17:17

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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