1

I have a custom post type that is going to use a loop in a couple different places and I wanted to make maintaining those loops easier. I remembered that get_template_part() is available and figured this would be an optimal time to get used to it. However, what has me at a standstill is how to set up a file for the loops so that I can call the specific parts accurately.

Google, thus far, has been not helpful in understanding how get_template_part() and twentyten's loop.php actually work and call the 3 parts. I have it duplicated, stripped down and ready for altering for my CPT version, but that is it thus far.

Any help?

2 Answers 2

1

Codex has very decent explanation of get_template_part() logic:

Assuming the theme folder is wp-content/themes, that the parent theme is twentyten, and the child theme is twentytenchild, then the following code --

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

will do a PHP require() for the named files in this order:

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

  2. wp-content/themes/twentytenchild/loop.php

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

  4. wp-content/themes/twentyten/loop.php

Basically you can have more generic templates and more specialized templates. Specialized template is looked up before generic. That is pretty much everything about it.

1
  • thanks for chiming in. After a bit of frustration, i'm just going to do wp_queries instead.
    – tw2113
    Commented Jan 25, 2011 at 17:15
1

I have always just used it to include files which are within my theme directory.

So, if i have a folder in the theme dir called functions that has a file called utils.php ( wp-content/themes/mytheme/functions/utils.php ) then you can just include the file like so

get_template_part( 'functions/utils' ); // includes /functions/utils.php - .php gets added automatically

or files in root of theme

get_template_part( 'sidebar' ); // includes sidebar.php

I don't really get the 2 parameters way of doing it. It works fine for me to just use the dir path structure to files and just leave of the file type.

1
  • I've never tried using a directory there - that's a nifty trick! Thanks for inspiration!
    – Michal Mau
    Commented Feb 1, 2011 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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