I don’t theme, generally speaking. I’m not bad at it, I just don’t have that particular aspect of ‘design’ in my head where I can create out of nothing. What I can do (and do do) is tweak the heck out of themes. This means I’m sure, if you theme, you know all this and think I’m a bit silly. That’s okay.

One thing I hate about code is duplicated effort. I don’t mind including the same library in 100 different projects, but I hate when I have to use the exact same code over and over in multiple files. Like I did in my theme.

I talked about using Chart.js in my theme before, and in that work I made use of get_template_part() to show the includes. It actually doesn’t look like that any more. Here’s the original code:

function lez_stats_footer() {
    get_template_part( 'stats' );
}

And here’s the current code:

function lez_stats_footer() {
	if ( is_page( 'stats' ) ) {
		get_template_part( 'inc/statistics', 'base' );
	}
	if ( is_page( 'death' ) ) {
		get_template_part( 'inc/statistics', 'death' );
	}
	if ( is_page( 'characters' ) ) {
		get_template_part( 'inc/statistics', 'characters' );
	}
	if ( is_page( 'shows' ) ) {
		get_template_part( 'inc/statistics', 'shows' );
	}
}

While get_template_part( 'stats' ) calls the file stats.php, the call to get_template_part( 'inc/statistics', 'shows' ); gets /inc/statistics-shows.php instead.

This lets me organize things a little better. My /inc/ folder is filled with this:

archive-characters.php
archive-shows.php
excerpt-characters.php
excerpt-shows.php
sidebar-characters.php
sidebar-shows_archive.php
sidebar-shows_single.php
statistics-base.php
statistics-characters.php
statistics-code.php
statistics-death.php
statistics-shows.php

As the help doc says, this makes it easy for a theme to reuse sections of code and that’s actually what I’m doing.

I have a call to get_template_part( 'inc/archive', 'characters' ); in multiple files, including some page templates but also in my category and taxonomy lists.

if ( get_post_type( get_the_ID() ) === 'post_type_characters' ) {
	get_template_part( 'inc/archive', 'characters' );
}

if ( get_post_type( get_the_ID() ) === 'post_type_shows' ) {
	get_template_part( 'inc/archive', 'shows' );
}

That means if it’s a show, use one format, and if it’s a character use another.

But…

One of the things I do is a little weird. On the single display pages for shows, I loop through all the characters related to the show and display them. There’s custom post meta to link the two, and what I do on the single page (single-post_type_shows.php) is grab the value for the post-meta of ‘shows’ on every single character page, and if the show ID is the same as the post ID of the page you’re on, add the character to an array.

This is not very efficient. I know. It’s basically running an extra loop on every ‘show page’ load and collecting this query. What’s important here though is the array:

$havecharacters[$char_id] = array(
	'id'      => $char_id,
	'title'   => get_the_title( $char_id ),
	'url'     => get_the_permalink( $char_id ),
	'content' => get_the_content( $char_id ),
);

This is important because I needed to pass this data through to my template part. On the template page for characters, I call get_template_part( 'inc/excerpt', 'characters' ); and it works fine because it knows exactly what page it’s on and calling get_the_post_thumbnail() will work because of what show it is. But when I call it from the shows page, though, it tried to use the values based on the show page, not the character.

What I needed to do was pass the parameters of the character array to the excerpt. And you can’t do that with get_template_part but you can with a different call.

include(locate_template('inc/excerpt-characters.php'));

This passed through my array, which let me call it with a nice for-loop :

if ( empty($havecharacters) ) {
	echo '<p>There are no characters listed for this show.</p>';
} else {
	foreach( $havecharacters as $character ) {
	?>
		<ul class="character-list">
			<li class="clearfix"><?php include(locate_template('inc/excerpt-characters.php')); ?></li>
		</ul>
	<?php
	}
}

And now my excerpt template can use $character['title'] and everything looked great on the show pages.

They did not, however, look so great on the character page, which used get_template_part remember. This was simply because the array for $character was empty. In order to make this work for the single character page, I had to recreate the array. Only I did it a little differently.

if ( empty($character) ) {
	$character = array(
		'id'      => get_the_ID(),
		'title'   => get_the_title(),
		'url'     => get_the_permalink(),
		'content' => '',
		'ischar'  => true,
		'shows'  => get_post_meta( get_the_ID(), 'lezchars_show', true ),
	);
}

The reason I wiped out the content (which held get_the_content in the original array, remember) is that on a single character page, WordPress knows to show the content, so I don’t need to double that work. I also added in new array items for ischar and shows so I could check if ( $character['ischar'] !== true ) {...} in some places and show slightly different content depending on what page I’m on.

Whew.

Reader Interactions

%d bloggers like this: