Custom fields in Meta Title

This topic contains 4 replies, has 3 voices, and was last updated by  Paolo 7 years, 10 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket

Tagged: 

  • Author
    Posts
  • #218162

    Utsav Sarkar
    Expired Member
    Post count: 29

    I have a custom field called clinic_places and I want that to be in the Meta title. I’ve tried the following but none of them worked:

    %%clinic_places%%
    %%cf_clinic_places%%
    %%cf_geodir_clinic_places%%
    %%geodir_clinic_places%%

    I also want the address to be in Meta Title. I’ve tried the following and did not work

    %%Address%%
    %%cf_Address%%
    %%geodir_Address%%

    Please help!

    #218240

    Giri
    Expired Member
    Post count: 3155

    Hi there, if you want to display custom fields in title, then you need to use custom code in your child theme.

    Since you said you have custom field called clinic_places

    I would try like this.

    
    
    add_filter('pre_get_document_title', 'geodir_custom_fields_page_title', 100);
    add_filter('wp_title', 'geodir_custom_fields_page_title', 100, 2);
    
    function geodir_custom_fields_page_title($title = '', $sep = '') {
        if (geodir_is_page('detail')) {
            global $post;
            if (isset($post->clinic_places)) {
                $title = str_replace('%%cf_clinic_places%%', $post->clinic_places, $title);
            }
        }
        return $title;
    }

    Please note, custom fields only work with detail pages. So you should use custom field only for “Details page meta title” setting in Title and Meta

    If you would like to display clinic places at the end of the title

    Just use this format

    
    
    
    %%title%% %%sep%% %%cf_clinic_places%%

    Basically my code replaces %%cf_clinic_places%% text with origin custom field value.

    If you have multiple post types with different custom fields then it gets complicated.

    #218729

    Utsav Sarkar
    Expired Member
    Post count: 29

    hmm I added that piece of code to my functions.php file in the Directory Starter theme and it still didnt work.

    #218733

    Utsav Sarkar
    Expired Member
    Post count: 29

    Oh nevermind I made a small modification to your code above and now it worked:

    
    
    
    add_filter('pre_get_document_title', 'geodir_custom_fields_page_title', 100);
    add_filter('wp_title', 'geodir_custom_fields_page_title', 100, 2);
    
    function geodir_custom_fields_page_title($title = '', $sep = '') {
        if (geodir_is_page('detail')) {
            global $post;
            
            $title = str_replace('%%cf_clinic_places%%', $post->geodir_clinic_places, $title);
            
        }
        return $title;
    }
    
    #218736

    Paolo
    Site Admin
    Post count: 31206

    Thanks for letting us know 🙂

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

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket