Helpful utilities - 3ev/wordpress-core GitHub Wiki
Wordpress Core provides a few utility functions for use in your templates. They typically patch holes that are missing in the Wordpress core.
###Template partials
Whilst get_template_part() is ok for template includes, it isn't very robust.
Wordpress Core provides tev_partial(), which allows you to specify the path to an include file in the current theme directory, and pass variables to it if required.
####Usage
in theme/index.php:
<?php echo tev_partial('partials/top', array('title' => 'Index Title')); ?>in theme/partials/top.php:
<h1><?php echo $title; ?></h1>###Generating breadcrumbs
Wordpress doesn't provide a built-in utility for generating page breadcrumbs, and you typically have to use a plugin to provide the functionality.
Wordpress Core provides this functionality through tev_app()->fetch('template_extras')->breadcrumbs(). This helper method will generate a breadcrumb list for the current page, which includes links and titles. This list is returned as an array, so you can render it how you'd like.
For example:
<?php $crumbs = tev_fetch('template_extras')->breadcrumbs(); ?>
<ul>
<?php foreach ($crumbs as $crumb): ?>
<li>
<?php if ($crumb['url']): ?>
<a href="<?php echo $crumb['url']; ?>" title="<?php echo $crumb['title']; ?>">
<?php endif; ?>
<?php echo $crumb['title']; ?>
<?php if ($crumb['url']): ?>
</a>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>Take a look at the source for the method if you'd like to see what logic is employed for generating the breadcrumb list.
####Overriding the default logic
If you'd like to override the default logic for rendering breadcrumbs, you can provide an array of callback functions (or one single callback function) as the sole method parameter. These callback functions should return a hash containing url and title if they match your logic, otherwise they should just return null.
Each callback function is applied in the order it was registered, and the first one to return something will be used as the breadcrumb for the current page. If none of the callbacks return anything, the default breadcrumb logic will be used.
Note: The 'Home' breadcrumb is always returned first in the list, even with custom crumbs.
For example:
<?php $crumbs = tev_fetch('template_extras')->breadcrumbs(function() {
$page = tev_post_factory();
if ($page->getId() === 123) {
return array(
'title' => 'My Custom Crumb',
'url' => 'http://www.google.com/'
);
}
}); ?>###Archive page headings
Wordpress Core provides a simple utility method for auto generating titles on post archive pages, through tev_fetch('template_extras')->archiveTitle(). The method accepts some optional config, so check the API docs for how it works.