As some of you might know, the get_template_part() -function of WordPress doesn’t allow you to pass variables. This can be quite annoying sometimes when you want to include a template several times, but each time with different parameters.
Imagine a situation for example where you want to show 3 category blocks next to each other, that each contain a link to the category, as well as the latest article in that specific category. If you don’t want to make your templates all too complex with all kind of PHP functions (like most of the solutions for this problem on the Internet do), how would you do this?
Well, I ran into the same situation and here’s how I fixed it:
The method
I hope you already learned how to keep your WordPress themes tidy and organised. If not, I suggest you read this article about before you continue reading.
In your theme class, we create a method called getTemplatePart() which works the same as the default get_template_part() -function of WordPress, but with one addition: it can accept parameters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class MyTheme { /** * Get template part with variables * @param $part * @param $vars */ function getTemplatePart($part, $vars) { if(is_array($vars)) { foreach($vars as $key => $value) { $key = '_' . $key; $$key = $value; } } include(locate_template($part . '.php', false, false)); } } |
Notice the little trick I did here by creating dynamic variables in PHP by writing $$key = $value . No, it’s not a typo: the double dollar signs allows us to create variables whose name is defined by our template. For example, if the value of $key is ‘foo’ and the value of $value is ‘bar’, our included template part will have a variable called $_foo with the value ‘bar’ (the underscore is added one line before, but you can omit that if that’s your thing). It’s that simple!
The implementation
You can use this implementation in your template like this:
1 2 3 4 5 |
<div id="categories"> <?php MyTheme::getTemplatePart('partials/category', array('slug' => 'magento', 'last' => false)); ?> <?php MyTheme::getTemplatePart('partials/category', array('slug' => 'webdevelopment', 'last' => false)); ?> <?php MyTheme::getTemplatePart('partials/category', array('slug' => 'wordpress', 'last' => true)); ?> </div> |
The template file (located in partials/category.php ) can look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $_category = get_category_by_slug($_slug); ?> <div class="category<?php if($_last): ?> last<?php endif; ?>"> <div class="details"> <p> <a href="<?php echo get_category_link($_category); ?>"><strong><?php echo $_category->name; ?></strong></a> - <?php echo $_category->description; ?> <em class="count">(<?php echo $_category->category_count; ?> articles)</em> </p> </div> <div class="lastpost"> <h4>Last post in this category:</h4> <?php $latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($_category->cat_ID))); $latest_cat_post->the_post(); ?> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <?php the_excerpt(); ?><a href="<?php the_permalink(); ?>">read further ...</a> </div> </div> |
Note the loading of the proper category according to $_slug : a variable assigned with a value in our main template. Also the variable $_last is used to determine whether or not we should add an additional class to our wrapper.
Visitors give this article an average rating of 4.4 out of 5.
How would you rate this article?
★ ★ ★ ★ ★