Support

Account

Home Forums Front-end Issues ACF Fields in page title

Helping

ACF Fields in page title

  • Hey

    We are trying to tailor the page title dependant on the following:

    1 – Viewing a single custom post type.
    2 – A value of a field on that post type = true.

    I cannot get the right hook, in the right order to both catch the page title and change it with ACF data.

    We want to be able to hide the page title for a post type if “hide_details” is true. If the user has the “show_details” field set to true this is overridden and we show our “premium” user the private content.

    Any tips or guidance on how to do this?

  • Hi

    Have you tried the the_title filter? this filter allows your to do something to the post title before it’s returned.
    https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title

    So, your code would look something like:

    
    
    add_filter('the_title', 'maybe_use_premium_acf_title', 15, 2);
    
    function maybe_use_premium_acf_title($title, $post_id) {
        // if it's not the cpt single, then don't do no nothing
        if (! is_singular('your_post_type')) {
            return $title;
        }
    
        // if 'hide_details' is not checked, then don't do no nothing either
        if (! get_field('hide_details')) {
            return $title;
        }
    
        return get_field('premium_title_field_name');
    }
    

    Cheers 😎

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘ACF Fields in page title’ is closed to new replies.