Preventing output of empty entry footer markup
In WordPress themes, single posts typically have the following information in the entry footer:
- The category list (if there are more than one categories sitewide)
- The tag list (if there are tags assigned)
- The edit link (if logged in)
In a lot of themes, even if all of the above are not true, an empty entry-footer div is still output in the markup, like this:
<div class="entry-footer">
<!-- blank space -->
</div>
If styles, like padding and borders, are applied to entry-footer div, with the expectation that stuff will be inside of it, things could look weird.
Even if not, to keep HTML code as clean as possible, it should only be output on the page when absolutely necessary.
Step 1: Do you have a “categorized blog” conditional function?
If you built your theme off of Underscores, you already do.
If not, take a moment to copy the following functions in your theme’s functions.php file:
function themeslug_categorized_blog() {
if ( false === ( $all_the_cool_cats = get_transient( 'themeslug_categories' ) ) ) {
$all_the_cool_cats = get_categories( array(
'fields' => 'ids',
'hide_empty' => 1,
'number' => 2,
) );
$all_the_cool_cats = count( $all_the_cool_cats );
set_transient( 'themeslug_categories', $all_the_cool_cats );
}
if ( $all_the_cool_cats > 1 ) {
return true;
} else {
return false;
}
}
function themeslug_category_transient_flusher() {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
delete_transient( 'themeslug_categories' );
}
add_action( 'edit_category', 'themeslug_category_transient_flusher' );
add_action( 'save_post', 'themeslug_category_transient_flusher' );
Be sure to replace themeslug with your own theme slug.
Step 2: Wrap the entry-footer markup in conditional tags
<?php if ( themeslug_categorized_blog() || get_the_tag_list() || get_edit_post_link() ) : ?>
<footer class="entry-footer">
<?php themeslug_entry_footer(); ?>
</footer><!-- .entry-footer -->
<?php endif; ?>
If any of the conditions are true, the entry-footer markup will display. If not, an empty div will not be output, and your code will be slightly cleaner.