-
ErnestW
Hello. I would like to make the site title (or the site description) set up in the customizer become the h1 of my home page.
My website is a one pager, the content title is hidden with an element and the first big title is a slogan, so not really suitable for h1. If I could add the h1 tag to the site branding (or the site description) that would be great. Thanks! -
Fernando
Hi ErnestW,
Try adding this snippet:
add_filter('generate_site_description_output', function($desc){ return sprintf( '<h1 class="site-description"%2$s> %1$s </h1>', html_entity_decode( get_bloginfo( 'description', 'display' ) ), // phpcs:ignore 'microdata' === generate_get_schema_type() ? ' itemprop="description"' : '' ); },10);
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
-
ErnestW
Thanks a lot, this has been incredibly helpful!
One last thing: do you think it is possible to concatenate “site title + site description” when generating the H1? -
Fernando
Try replacing the code with this:
add_filter('generate_site_description_output', function($desc){ return sprintf( '<h1 class="main-title"%4$s> <a href="%2$s" rel="home"> %3$s %5$s </a> </h1>', ( is_front_page() && is_home() ) ? 'h1' : 'p', esc_url( apply_filters( 'generate_site_title_href', home_url( '/' ) ) ), get_bloginfo( 'name' ), 'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '', html_entity_decode( get_bloginfo( 'description', 'display' ) ) ); },10);
-
ErnestW
It works for the h1 tag, but it also replaces the site description itself…
-
Fernando
That code should concatenate the title and the description in one H1. What occurred when you added the code?
-
ErnestW
The h1 was perfect (concatenated) but the actual site description on the front page got replaced with the concatenated string.
-
Fernando
I thought that was the goal? You’ll just need to disable the Site title so that the concatenated string remains.
-
ErnestW
I am sorry, I now realise I have been a bit confusing. I wanted to leave the frontend untouched and generate a h1 title that concatenated the two, but I now see that this is a mistake. I will use the firs snippet that worked very well.
Thanks again Fernando!
-
Fernando
I see. You’re welcome, Ernest!
-
ErnestW
Hello! Just a small additional question. The snippet provided in the first reply of this topic was very handy to assign the h1 tag to the site description.
Is it possible to tweak it to do the same, but with the site title instead, and ONLY on the home page? I have declared the site title in the customizer and it is set to hidden. I’d like to give it the h1 tag (still keeping it hidden). Thanks a lot!
-
David
Hi there,
if you hide the Site Title in the customizer then the theme doesn’t print any HTML for it to be set to a H1.
Why do you want to add a hidden H1 to your page ?
-
ErnestW
Ah, I see…
The page I designed doesn’t have a h1 tag, so I was wondering if I could use the site title. If I make it visible, what is the snippet needed to assig it the h1 tag?
Thanks a lot! -
David
Is your homepage a static page ? ( ie. not set to show latest posts )
-
ErnestW
Yes!
-
David
Try this PHP Snippet:
add_filter( 'generate_site_title_output', function( $output ) { return sprintf( '<%1$s class="main-title" itemprop="headline"> <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( '/' ) ) ), get_bloginfo( 'name' ) ); });
- You must be logged in to reply to this topic.