[Resolved] Apply H2 title on front page, H1 on archive pages

Home Forums Support [Resolved] Apply H2 title on front page, H1 on archive pages

Home Forums Support Apply H2 title on front page, H1 on archive pages

  • This topic has 5 replies, 2 voices, and was last updated 6 months ago by Fernando.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2348081
    Fergal

    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 );
    
    #2348096
    Fernando
    Customer Support

    Hi Fergal,

    To clarify, if it’s paged, it should be H1, and if not – H2?

    #2348450
    Fergal

    Hey Fernando,

    That should work!

    #2349057
    Fernando
    Customer Support

    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 );
    #2349487
    Fergal

    Nailed it! Thanks Fernando.

    #2350132
    Fernando
    Customer Support

    You’re welcome Fergal!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.