Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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.

share|improve this answer
    
thanks for chiming in. After a bit of frustration, i'm just going to do wp_queries instead. –  tw2113 Jan 25 '11 at 17:15

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.

share|improve this answer
    
I've never tried using a directory there - that's a nifty trick! Thanks for inspiration! –  Michal Mau Feb 1 '11 at 23:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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