[Resolved] Replace header post title with custom field

Home Forums Support [Resolved] Replace header post title with custom field

Home Forums Support Replace header post title with custom field

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1153122
    Todd

    Hello, I can see how to get a custom field to show up in a header. But is there a simple way to have a custom field IF present replace the post title? So the post title is the default if there is no custom field populated on a page. If there is a custom field populated then it replaces the title.

    #1153472
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Try this:

    add_filter( 'generate_page_hero_post_title', function( $title ) {
        $custom_field = get_post_meta( get_the_ID(), 'your_custom_field', true );
    
        if ( $custom_field ) {
            return $custom_field;
        }
    
        return $title;
    } );

    Let me know πŸ™‚

    #1153497
    Todd

    Tom, I put a preview test link in the original Support topic.

    It seems to show the custom field twice? I assume that I am replacing the ‘your_custom_field’ with my field name in the snippet so it looks like this:

    add_filter( 'generate_page_hero_post_title', function( $title ) {
        $custom_field = get_post_meta( get_the_ID(), 'head_pg_header', true );
    
        if ( $custom_field ) {
            return $custom_field;
        }
    
        return $title;
    } );

    The header title looks like this:

    <h1>{{post_title}}</h1>
    <h1>{{custom_field.head_pg_header}}<h1>
    #1153500
    Tom
    Lead Developer
    Lead Developer

    You only need this in the header now:

    <h1>{{post_title}}</h1>

    #1153503
    Todd

    Worked perfectly. Best customer service in the world!

    #1153508
    Tom
    Lead Developer
    Lead Developer

    Awesome! Glad I could help πŸ™‚

    #1161340
    Todd

    I noticed one other thing I would like to fix with this header solution, if I could. It seems that if there is a page that can not be found or a search with no result or a search with a result, the custom field variable that I have for a subhead shows in the header shows up.

    https://customairco.com/qwiktest-03/
    https://customairco.com/?s=monkey
    https://customairco.com/?s=rheem

    It does not show on regular page through out the rest of the site:
    https://customairco.com/rheem-hvac-spring-2020-offer/

    Thank you.

    #1162098
    Tom
    Lead Developer
    Lead Developer

    Custom fields are only available on singular pages/posts.

    It would be better to create a separate Element for archives (including search results) without the custom field.

    #1162427
    Todd

    I duplicated the header and made the change. Works well on search and 404 templates. Just curious when this is run on an archive it works well but seems to pick up the title of the first page like you see on the first search example for “Rheem”, all the other examples seem fine.

    The header is: <h1>{{post_title}}</h1>

    #1163134
    Tom
    Lead Developer
    Lead Developer

    We may have to directly target search results to change what {{post_title}} outputs. I’ll look into this for GPP 1.10.

    #1163167
    Todd

    Thanks Tom, appreciate it.

    #1163582
    Tom
    Lead Developer
    Lead Developer

    No problem! Appreciate the heads up πŸ™‚

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