[Resolved] Headings h1

Home Forums Support Headings h1

This topic contains 9 replies, has 2 voices, and was last updated by  Tom 6 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #486544

    Roman

    Hi there

    On category and post pages in heading h1 shows category name or post title.
    I have two questions:

    1. For categorys. How to remove this heading entirely (with h1 tag)?
    2. For both. How to replace h1 tag contents by my custom field from ACF?

    GeneratePress 2.0.2
    GP Premium 1.5.6
    #486910

    Tom Lead Developer

    Hi there,

    Not too sure what you mean by post pages? Single posts?

    For the categories, you can do this:

    add_filter( 'get_the_archive_title', 'tu_custom_archive_title' );
    function tu_custom_archive_title( $title ) {
        if ( is_category() ) {
            // Your custom ACF field here
        }
    
        return $title;
    }
    #487931

    Roman

    Yep, single posts.

    This code doesn’t work, but thanks for directing me to the right way 🙂

    Here is the code for categories (replaces title with advanced custom field):

    add_filter( 'single_cat_title', 'tu_custom_archive_title' );
    function tu_custom_archive_title( $title ) {
    	$term = get_queried_object();
    	$mycattitle = get_field('acf_category_title', $term);
    	if ( $mycattitle != '' ) {
    		return $mycattitle;
    	}
    	else {
    		return $title;
    	}
    }

    Can you help me do the same for single posts?

    #488086

    Tom Lead Developer

    For single posts, I believe you would use the_title filter.

    Let me know if that does it or not 🙂

    #490842

    Roman

    Emmm. It works, but it also replaces text of almost all links with my custom field 🙂 Navigation, sidebar, prev and next post links – all have my title instead link text.

    I tried to move the code from snippet to hook (before content). Now navigation is fine, but all that after this hook has changed.

    Here is the code:

    add_filter( 'the_title', 'tu_custom_post_title' );
    function tu_custom_post_title( $title ) {
    	$term = get_queried_object();
    	$myposttitle = get_field('acf_post_title', $term);
    	if ( $myposttitle != '' ) {
    		return $myposttitle;
    	}
    	else {
    		return $title;
    	}
    }
    #490971

    Tom Lead Developer

    Hi there,

    Try this instead:

    add_filter( 'the_title', 'tu_custom_post_title' );
    function tu_custom_post_title( $title ) {
    	$term = get_queried_object();
    	$myposttitle = get_field('acf_post_title', $term);
    	if ( $myposttitle != '' && in_the_loop() ) {
    		return $myposttitle;
    	}
    	else {
    		return $title;
    	}
    }
    #492485

    Roman

    Better, near perfect ) But prev and next links still have my title. Because they have %title too, I think.

    #492689

    Tom Lead Developer

    I wonder if adding this would fix it:

    add_action( 'generate_after_entry_header', 'tu_remove_title_filter' );
    function tu_remove_title_filter() {
        remove_filter( 'the_title', 'tu_custom_post_title' );
    }

    Bit of a long shot.. Let me know 🙂

    #495472

    Roman

    Now everything is perfect ) Thanks!

    #495805

    Tom Lead Developer
Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.