Site logo

Archived topic

Insert text before tag archive title

11 replies · Started by Tom on May 25, 2022

Viewing posts 1–12 of 12

I need to insert some text before the <h1> tag archive title on every tag page.

For example let's say the tag is called "rhubarb". On the tag archive page for all things "rhubarb", I need to change the <h1> from <h1>Rhubarb</h1> to <h1>Added text Rhubarb</h1>

To be clear - the "Added text" need to be added before all other tag titles as well.

The site is a dev site I'm converting from an older theme.

The previous theme used this functions.php snipped to achieve the above:

---------------

	// Tag Archive
	if ( is_tag() ) {

		$title = __( 'Added text ', 'woothemes' ) .  single_tag_title( '', false );
	}

	// Allow for external filters to manipulate the title value.
	$title = apply_filters( 'woo_archive_title', $title, $before, $after );

	if ( $echo )
		echo $title;
	else
		return $title;

} // End woo_archive_title()

Please can you let me know how I can achieve the same in GeneratePress pro using a functions snippet?

Thanks.

so I cannot use a block element since there’s no way to use a block element in a tag archive.

Why would you think that way? A block element can be assigned to all archives, including tag archive.

I would think that way because it isn't possible?

If you think it is possible, please provide a complete walk through and I'll be happy to try it.

Otherwise, please could you provide a functions.php code snippet, as requested, because the documentation on Generatepress hooks and filters isn't very clear in this respect and I'm asking for support with this.

Thanks for your understanding.

I think a smart person like you should be able to figure out after watching this video, it's a step by step tutorial :)
https://youtu.be/L1KtrKJIFzk

I believe your suggestion requires the block element to disable (strip out) the existing tag archive title and replace it with a new dynamic title as defined by the block element, which is then printed as an <h1>.

This isn't what I'm looking for.

I need to keep the existing tag title, but simply add (print) text before it.

On the site I'm working on it's very important that the existing tag titles are retained since this data is used for various filters and external applications. It's no use if the entire tag title is changed. That's why the text needs to be added to the <h1>, and not the tag title changed. I hope this is clear.

The best way to do this would be a functions snippet as I suggested, which is why I'm asking for your support assistance with this.

I see.

So the tag archive is product tag archive? not post tag archive?

No, it's a post tag archive.

Try this, just replace Your text here with your text.

add_action('wp',function() {
	if (is_tag()) {
             remove_action( 'generate_archive_title', 'generate_archive_title' );
             add_action( 'generate_archive_title', 'generate_custom_tag_archive_title' );
	}
});
	/**
	 * Build the archive title
	 *
	 * @since 1.3.24
	 */
	function generate_custom_tag_archive_title() {
		if ( ! function_exists( 'the_archive_title' ) ) {
			return;
		}
		?>
		<header <?php generate_do_attr( 'page-header' ); ?>>
			<?php
			/**
			 * generate_before_archive_title hook.
			 *
			 * @since 0.1
			 */
			do_action( 'generate_before_archive_title' );
			?>

			<h1 class="page-title"> Your text here
				<?php the_archive_title(); ?>
			</h1>

			<?php
			/**
			 * generate_after_archive_title hook.
			 *
			 * @since 0.1
			 *
			 * @hooked generate_do_archive_description - 10
			 */
			do_action( 'generate_after_archive_title' );
			?>
		</header>
<?php
	}

That's perfect. Thanks so much for your help with this. :)

No problem :)

This archived topic is closed to new replies.