Site logo

Archived topic

Change Custom Post Type Archive page when using Block Element for hero image

5 replies · Started by Anastasiya on December 8, 2022

Viewing posts 1–6 of 6

Hi,
I am using Dynamic Data in Block element for custom post type archives page hero.
Custom post Type archive displays the name "Vimeo Videos" for the title element and I want to change it to something like :"All Videos".
I tried using the following code but it has no effect on the title of the archive page:

add_filter( 'get_the_archive_title', function( $title ) {
    if ( is_post_type_archive( 'vimeo-video' ) ) {
        $title = 'All Videos';
    }

    return $title;
}, 50 );

My Question is: does the code above affect Dynamic data when creating page hero block element or not?

Hi Anastasiya,

You'll need a different code if you're working with a GB Headline Block on a Page Hero.

Try adding change-title to the class list of the Dynamic Headline Block, and then add this Snippet:

add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
	if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'change-title' ) !== false && is_post_type_archive( 'vimeo-video' ) ) {
		return 'All Videos';
	}
	return $content;
}, 10, 3);

Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

Alternatively, you can just create a separate Page Hero just for that Archive page.

Thanks Fernando,

I tried the code that you suggested but it had no effect on the output. The reason is probable because I didn't explain myself correctly. I am using Dynamic text to output the archive title because Dynamic date toggle doesn't output taxonomy archive titles.

Dynamic text

Is there a filter that can be used for Dynamic text? I can't find any of these filters in the list of official filters on the GP site.

(Note: I know that I can create a custom archive title but I am trying to limit the number of elements that are created on the website if possible.)

Hi there,

Can you try the below snippet?

add_filter( 'render_block', function( $block_content, $block ) {
    if (! empty(  $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'change-title' ) !== false && is_post_type_archive( 'vimeo-video' ) ) {
        $block_content = 'All Videos';
    }
  return $block_content;
}, 10, 3);

Thanks Ying, this code worked. there was small type in your answer (in case someone else will use the code):

 && strpos( $block['attrs']['className'], 'change-title' ) !== false && is_post_type_archive( 'vimeo-video' ) ) {
        $block_content = 'All Videos';

Oops, I've corrected my code :)

Glad it works!

This archived topic is closed to new replies.