Adding Tags To Understrap Pages

Understrap for some reason has code specifically for suppressing tags from appearing on pages despite them not having them by default.

This confused me like a man setting off to dig a river through a desert only to hit a sea wall…

Sea Wall Demolition

The first step to fixing this is to import from the Understrap parent theme “inc/template-tags.php”.

Then find this section:

// Hide category and tag text for pages.
		if ( 'post' === get_post_type() ) {
			/* translators: used between list items, there is a space after the comma */
			$categories_list = get_the_category_list( esc_html__( ', ', 'understrap' ) );
			if ( $categories_list && understrap_categorized_blog() ) {
				/* translators: %s: Categories of current post */
				printf( '<span class="cat-links">' . esc_html__( 'Posted in %s', 'understrap' ) . '</span>', $categories_list ); // WPCS: XSS OK.
			}
			/* translators: used between list items, there is a space after the comma */
			$tags_list = get_the_tag_list( '', esc_html__( ', ', 'understrap' ) );
			if ( $tags_list ) {
				/* translators: %s: Tags of current post */
				printf( '<span class="tags-links">' . esc_html__( 'Tagged %s', 'understrap' ) . '</span>', $tags_list ); // WPCS: XSS OK.
			}
		}

And replace it with:

// Hide category for pages.
		if ( 'post' === get_post_type() ) {
			/* translators: used between list items, there is a space after the comma */
			$categories_list = get_the_category_list( esc_html__( ', ', 'understrap' ) );
			if ( $categories_list && understrap_categorized_blog() ) {
				/* translators: %s: Categories of current post */
				printf( '<span class="cat-links">' . esc_html__( 'Posted in %s', 'understrap' ) . '</span>', $categories_list ); // WPCS: XSS OK.
			}
		}
		
		/* translators: used between list items, there is a space after the comma */
		$tags_list = get_the_tag_list( '', esc_html__( ', ', 'understrap' ) );
		if ( $tags_list ) {
			/* translators: %s: Tags of current post */
			printf( '<span class="tags-links">' . esc_html__( 'Tagged %s', 'understrap' ) . '</span>', $tags_list ); // WPCS: XSS OK.
		}

Next import from the Understrap parent theme “loop-templates/content-page.php” (if you haven’t already).

Find this section:

	<footer class="entry-footer">

		<?php edit_post_link( __( 'Edit', 'understrap' ), '<span class="edit-link">', '</span>' ); ?>

	</footer><!-- .entry-footer -->

And replace it with:

	<footer class="entry-footer">
			<?php understrap_entry_footer(); ?>
	</footer><!-- .entry-footer -->

River Digging

Finally you can actually add tags to pages in “functions.php”:

/**
 * Add Tags to Pages.
 */
function tags_support_all() {
	register_taxonomy_for_object_type('post_tag', 'page');
}

/**
 * Ensure all tags are included in queries.
 */
function tags_support_query($wp_query) {
	if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
}

/**
 * Tag hooks.
 */
add_action('init', 'tags_support_all');
add_action('pre_get_posts', 'tags_support_query');

Leave a Reply

%d bloggers like this: