Archived topic
Apply H2 title on front page, H1 on archive pages
5 replies · Started by Fergal on September 19, 2022
Hi,
I have the following code that displays "All Jobs", "All Jobs Page 2", on the home page and paginated pages.
I'd like to set the "All Jobs" on the homepage to an H2 tag since I already have an H1. Using H1 on the paginated/archive pages works since it is the only H1 tag on those pages. Can you please help with this?
// job opening archive title
add_filter( 'get_the_archive_title', function( $title ) {
if(is_post_type_archive('job-opening')) {
the_post();
//global $page;
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$title = sprintf('All Jobs - Page %1$s', $paged);
rewind_posts();
}
return $title;
} );
// front page job opening archive title
add_filter( 'render_block', function( $block_content, $block ) {
if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'my-blog-title' ) !== false && is_paged() ) {
$myreplace1 = 'All Jobs';
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$myinsert1 = sprintf('All Jobs - Page %1$s', $paged);
$block_content = str_replace( $myreplace1, $myinsert1 , $block_content );
}
return $block_content;
}, 10, 2 );
Hi Fergal,
To clarify, if it's paged, it should be H1, and if not - H2?
Hey Fernando,
That should work!
Try this filter:
add_filter( 'render_block', function( $block_content, $block ) {
if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'my-blog-title' ) !== false && !is_paged() ) {
$myreplace1 = 'h1';
$myinsert1 = 'h2';
$block_content = str_replace( $myreplace1, $myinsert1 , $block_content );
}
return $block_content;
}, 10, 2 );
Nailed it! Thanks Fernando.
You're welcome Fergal!