2

I have a custom post type of 'competitions'. In order to customise the display for this CPT in, for example, category listings I am doing the following:

  • Create a category-competitions.php file
  • Create a content-competitions.php file
  • Within the loop in that file, call get_template_part('content-competitions', get_post_format() )'

This works, but I'm not sure this is the correct method. Is it necessary to include the whole category-competitions.php file to change that one line, or is there a built in way to achieve this that I am missing?

4

What you've done will include content-competitions-<post-format>.php. I don't know if that is what you want, but that is correct as far as structure goes.

I also don't know what you mean by "to change that one line", but get_template_part will use the two parameters to construct a file name/path so, yes, you do need to include all of it. Take a look at the explanation in the Codex:

<?php get_template_part( 'loop', 'index' ); ?>

will do a PHP require() for the first file that exists among these, in this priority:

        wp-content/themes/twentytenchild/loop-index.php
        wp-content/themes/twentyten/loop-index.php
        wp-content/themes/twentytenchild/loop.php
        wp-content/themes/twentyten/loop.php

What you get with get_template_part is

  1. "child or parent theme name", first one then the other
  2. plus "first parameter"
  3. Plus "-"
  4. plus "second parameter"
  5. plus ".php"

Or ...

  1. "child or parent theme name", first one then the other
  2. plus "first parameter"
  3. plus ".php"

... if nothing is found having the second parameter.

There is no shortcut. WordPress can't guess what file you want to include.

If you could name your files something like postformat.php you could get away with using only the first parameter though:

get_template_part( get_post_format() );

I don't know if that is a possibility as that post-format.php file would be used for any post having that named post format.

3
  • Thanks for your reply. As I said, it does work the way I have set it up. The "one line" refers to duplicating the entire category file in order to add "-competitions" into the get_template_part() function. You say wordpress can't guess what file I want, but the file I want could be identified purely by its name - if there was a better way of handling it. That was my question - "is there a better (more correct) way?" – EpF Jun 23 '13 at 17:40
  • 'That was my question - "is there a better (more correct) way?' -- And that is the question I answered. "No". There is no shortcut. If you don't provided the complete name WordPress cannot locate the file, though the second parameter is optional. You could save part of the filename to a variable but that is the only (maybe) simplification that I can think of. – s_ha_dum Jun 23 '13 at 17:50
  • Now I understand what get_template_part really does! – lowtechsun Jun 15 '17 at 16:07

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.