9. Important Theme Functions

In addition to all of WordPress’s theme functions (aka “Template Tags”), NOUVEAU provides some unique options and alternatives that help with theme development.

As mentioned earlier in this guide, all the classes and methods in the \NV\Utilities\ folder are available for developer use.

Namespace

All of NOUVEAU‘s theme/template functions are located in the class NV\Theme\Utilities\Theme. For ease of access, it’s recommended to include a use statement at the top of your template files (the following examples will assume you’ve done so).

The following are some of the most important functions…

Loading the Header & Footer

By default, WordPress wants header.php and footer.php to be in the theme root. Since these are technically template parts (albeit, specialized template parts), it is a little bit tacky to keep these files lumped in with the single-page templates.

To clean things up, NOUVEAU moves the header and footer files to a separate directory: parts/layout. Unfortunately, this means that WordPress’s get_header() and get_footer() functions no longer work. Instead, you need to use specialized (and more flexible), versions of these functions included with NOUVEAU.

<br />
<?php Theme::get_header(); ?><br />

<br />
<?php Theme::get_footer(); ?><br />

 

Output File Markers as HTML Comments

In WordPress, it is best practice to keep your template files as streamlined and reusable as possible. If you have been developing on WordPress for any amount of time, you may have run into a scenario where a piece of content does not look how you expect, and you’re not sure which template or template part is being used.

This handy function can be used to help you figure out which template file is being used to render a particular piece of content (but only if WP_DEBUG is enabled). It will simply output an HTML comment containing the file path (relative to the theme root).

<br />
<?php Theme::output_file_marker(__FILE__); ?><br />

 

Simplifying the Loop

NOUVEAU allows you to execute the loop with a single function call and a template part. None of the usual sloppy, inline, procedural stuff that quickly results in unreadable spaghetti code. It’s as simple as this…

<br />
<?php Theme::loop( 'parts/article', 'parts/article-empty' ); ?><br />

Since this function uses WordPress’s built-in get_template_part() function, you can just pass it a string with your template part, and that part is automatically used in the loop.

 

Simplifying a Custom Loop

The above simplicity can be applied to custom loops as well by using the \NV\Theme:custom_loop() function. It takes four parameters…

$custom_query

Required. Any custom WordPress query (a WP_Query object).

$part

Required. A template part to be used in the loop (string).

$no_part

Optional. A template part to be used when there are no results (string).

$var_name

Optional. The variable name you want to use to access loop data in your template part. Defaults to “query” (string).

<br />
<?php<br />
$movies = new WP_Query( array(<br />
    'post_type' => 'movies'<br />
) );<br />
Theme::custom_loop( $movies_query, 'parts/movie', 'parts/movie-empty', 'movie' );<br />
?><br />

Keep in mind that if you need to directly access your post data from your template part (besides the usual theme functions), you will need use the variable defined by the $var_name argument. For our movie loop above, it might look like this: $movie->post->post_title


This page was last updated on September 15, 2016 – it is currently considered COMPLETE.