Tutorials

How to Exclude Certain Tags from Single Post Tags in Kadence

Disclosure: This post may contain affiliate links – meaning I get a commission if you decide to make a purchase through my links, at no cost to you.

In the previous tutorial, I showed how Elements feature of Kadence can be used to add an affiliate disclosure automatically above the entry content on all posts that have a specific tag.

Let’s say you decide to use the “Affiliate Disclosure” tag for this, it will be shown at the bottom of single posts tagged “Affiliate Disclosure” and you most probably do not want that.

The current version of Kadence does not include an option in the Customizer to exclude specific tags from appearing on single posts. While we could simply use CSS and hide the unwanted ones, it is better to not print them in the HTML output rather than sweeping them under the rug.

So the way to go about this is to disable “Show Post Tags” in the Customizer, copy the code from Kadence theme files, modify it to exclude certain tags and hook this to kadence_single_after_inner_content.

Step 1

In the Customizer go to Blog Posts > Single Post Layout and turn off “Show Post Tags”.

Step 2

Add the following either in child theme‘s functions.php or as a Code Snippet:

add_action( 'kadence_single_after_inner_content', 'custom_entry_tags' );
/**
 * Exclude certain tags from single post tags.
 */
function custom_entry_tags() {
	if ( 'post' !== get_post_type() ) {
		return;
	} ?>

	<footer class="entry-footer">
		<?php
		$tags = get_the_tags();
		if ( ! is_array( $tags ) ) {
			return;
		}
		?>
		<div class="entry-tags">
			<span class="tags-links">
				<span class="tags-label screen-reader-text">
					<?php echo esc_html__( 'Post Tags:', 'kadence' ); ?>
				</span>
				<?php
				$exclude_these_term_ids = array(
				   229,
				);
				foreach ( $tags as $tag_item ) {
					if ( ! in_array( $tag_item->term_id, $exclude_these_term_ids ) ) {
						$tag_link = get_tag_link( $tag_item->term_id );
						echo '<a href=' . esc_url( $tag_link ) . ' title="' . esc_attr( $tag_item->name ) . '" class="tag-link tag-item-' . esc_attr( $tag_item->slug ) . '" rel="tag"><span class="tag-hash">#</span>' . esc_html( $tag_item->name ) . '</a>';
					}
				}
				?>
			</span>
		</div><!-- .entry-tags -->
	</footer><!-- .entry-footer -->
<?php }

In the above set the ID of the tag you want to exclude in

$exclude_these_term_ids = array(
	229,
);

To exclude multiple tags, separate them by commas like so:

$exclude_these_term_ids = array(
	229,
	423,
);

References

/themes/kadence/template-parts/content/entry_tags.php

/themes/kadence/template-parts/content/entry_footer.php

/themes/kadence/template-parts/content/single-entry.php

/themes/kadence/template-parts/content/single.php

https://wordpress.stackexchange.com/a/59482/14380

Similar Posts

Leave a Reply

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