Site logo

Archived topic

Dynamic Site Title Depending on if it's a post vs page?

1 reply · Started by bramtim on August 8, 2022

Viewing posts 1–2 of 2

Is there a way I change the site title depending on if the post type is post vs page? I would prefer not to have to create a child theme as I still want the updates from you all.

Use case. When its on the blog index or blog pages I want the site title to be "{SAAS_NAME} | Blog" but when its on non-blog pages I would like it to say just say, "{SAAS_NAME}".

Hi Bramtim,

There’s a filter for the site title: generate_site_title_output

Reference: https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/inc/structure/header.php#:~:text=generate_site_title_output

Example code:

add_filter( 'generate_site_title_output', function($output) {
	
	if( 1431 === get_the_ID() ) {
			$new_title = 'test title';
			$output = sprintf(
				'<%1$s class="main-title"%4$s>
					<a href="%2$s" rel="home">
						%3$s
					</a>
				</%1$s>',
				( is_front_page() && is_home() ) ? 'h1' : 'p',
				esc_url( apply_filters( 'generate_site_title_href', home_url( '/' ) ) ),
				$new_title,
				'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : ''
			);
	}

	return $output;
	
}, 10 );

Replace test title with your preferred title and 1431 with the page/post ID.

This archived topic is closed to new replies.