Archived topic
Change site title depending on the post type
3 replies · Started by Jesús on February 10, 2021
Hi!
I have 2 post types (restaurants and hotels) and I want to change the site title depending on the post type.
I want to show site title RESTAURANT on posts type "restaurant" and show site title HOTELS on post type "hotel".
Thanks!
Hi there,
You can filter the site title depending on post type using this PHP snippet.
add_filter('generate_site_title_output',function(){
$custom_post = get_post_type( get_the_ID() );
if(!$custom_post == 'post'){
if( $custom_post == 'slug_post_type' ){
$custom_title = $custom_post;
return 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( '/' ) ) ),
$custom_title,
'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : ''
);
}
}
});
Where 'slug_post_type' should be the slug of your custom post type for restaurant.
You can add in multiple if conditions for multiple post types.
Thanks Elvin!
Finally I try this:
add_filter('generate_site_title_output','jmd_filter_pagetitle');
function jmd_filter_pagetitle ($output){
$custom_post = get_post_type( get_the_ID());
if ( $custom_post == 'alojamiento' ) {
$custom_title = 'BARBAROSSA Suites';
return sprintf(
'<%1$s class="alojamiento-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( '/' ) ) ),
$custom_title,
'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : ''
);
}
else {
return $output;
}
}
Nice one. Thank you.
Have you resolved this? Let us know. :D