Reducing duplicity in the homepage loop while using the ephemera widget on the twentyfourteen child theme

-OR-

Say something once, why say it again?

Step one toward a better WordPress twentyfourteen child theme

Today, I launched my new child theme for WordPress’ new default theme, twentyfourteen, which will be a running work in progress on this site likely for the next few weeks leading to the official initial release of it in the WordPress theme repository.

As I’m developing it, I will post here about new features and ways I’ve chosen to craft the theme to my liking.

Some of the first adjustments I have made include tweaking the width of the theme to display better at full width on my 17″ laptop screen and adjust the themes colors to a custom palette. But I’m not finished in these departments, so I will wait until I am completely satisfied before I elaborate more here.

Today’s adjustment focuses on adjusting the main loop of the theme so that it doesn’t repeat content found in the ephemera widget floated to the right of the main content. The ephemera widget has come with the last few incarnations of the default theme, and allows the user to display posts of a certain post format of their choosing … However, that content, on default, is also displayed in the main loop, which means the day you post a new post in that format, identical content will run side-by-side. It’s a shame that a solution to this problem isn’t offered in the default theme twentyfourteen, but below, find the solution I implemented by creating a functions.php file for my new child theme and adding the following code to it (between the opening and closing PHP tags, of course):


function exclude_post_formats( $query ) {
	if ( $query->is_main_query() && $query->is_home() ) {
		$tax_query = array(
			array(
				'taxonomy' => 'post_format',
				'field' => 'slug',
				'terms' => array(
					'post-format-link'
				),
				'operator' => 'NOT IN',
			)
		);
		$query->set( 'tax_query', $tax_query );
	}
}
add_action( 'pre_get_posts', 'exclude_post_formats' );

This function will exclude posts with the link post format from the homepage loop query, because I prefer to have those posts display in the ephemera widget floated to the right of the main content, and the duplicity is irksome. I learned how to do this by reading the article Modifying the default WordPress loop to exclude specific post formats by Jeffrey Barke.

Initially, I tried doing this by duplicating the index.php file from twentyfourteen, and then using an advanced taxonomy query to tell the homepage loop what to display by telling it what NOT to display (as suggested by Roy Scribner and Otto); but this didn’t work, because the twentyfourteen theme takes a different approach to doing this via the pre_get_posts action in the file inc/featured-content.php … so the above function that runs along side it works great in my new twentyfourteen child theme, and filters out the post-format-link posts while keeping the other query features of the parent twentyfourteen theme in tact.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>