[Resolved] Insert text before tag archive title

Home Forums Support [Resolved] Insert text before tag archive title

Home Forums Support Insert text before tag archive title

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #2232601
    Tom

    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.

    #2232607
    Ying
    Staff
    Customer Support

    Hi Tom,

    You can easily achieve this using a block element – page hero:
    https://docs.generatepress.com/article/block-element-page-hero/

    When you enable the dynamic data for headline block, there’s field called before text, you can simply add the text in to that field.
    https://www.screencast.com/t/jvJ7VWFo

    #2232610
    Tom

    Sorry if I was not clear. I’m talking about a tag archive, not a page or a post; so I cannot use a block element since there’s no way to use a block element in a tag archive.

    More information on WordPress tags:
    https://developer.wordpress.org/reference/functions/single_tag_title/

    #2232624
    Ying
    Staff
    Customer Support

    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.

    #2232629
    Tom

    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.

    #2232637
    Ying
    Staff
    Customer Support

    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

    #2232638
    Tom

    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.

    #2232642
    Ying
    Staff
    Customer Support

    I see.

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

    #2232645
    Tom

    No, it’s a post tag archive.

    #2232659
    Ying
    Staff
    Customer Support

    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
    	}
    
    #2232668
    Tom

    That’s perfect. Thanks so much for your help with this. 🙂

    #2232678
    Ying
    Staff
    Customer Support

    No problem 🙂

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.