advanced custom fields - How to change the page title from functions.php

I have an ACF custom field that depends on the value I need to change the page title, however, I cannot affect the page

I have an ACF custom field that depends on the value I need to change the page title, however, I cannot affect the page title, it appears my code is running after the page title is generated.

To explain my requirements. This is a real estate site where some listings are confidential, they flag it as such in the admin, the frontend should only show a small amount of data to logged out users.

Heres the latest code ive tried:

add_filter('init', 'my_custom_title');
function my_custom_title( $title ){
    global $post;
    //var_dump($post);
    echo $post->ID;
    $hide_price = get_field("hide_price");
    echo get_field("beds",$post->ID);
    $view_hidden = get_field('view_hidden_listings', 'user_'. get_current_user_id() );
    if($view_hidden == 1) {
        $hide_price = 0;
    }
    if( $post->post_type == 'sales_listings' && $hide_price == 1) {
        return "Confidential";
    }
    echo $view_hidden;
}

Any help is greatly appreciated, I've been requesting support from the ACF guys but they are unable to help.

Thanks!

I have an ACF custom field that depends on the value I need to change the page title, however, I cannot affect the page title, it appears my code is running after the page title is generated.

To explain my requirements. This is a real estate site where some listings are confidential, they flag it as such in the admin, the frontend should only show a small amount of data to logged out users.

Heres the latest code ive tried:

add_filter('init', 'my_custom_title');
function my_custom_title( $title ){
    global $post;
    //var_dump($post);
    echo $post->ID;
    $hide_price = get_field("hide_price");
    echo get_field("beds",$post->ID);
    $view_hidden = get_field('view_hidden_listings', 'user_'. get_current_user_id() );
    if($view_hidden == 1) {
        $hide_price = 0;
    }
    if( $post->post_type == 'sales_listings' && $hide_price == 1) {
        return "Confidential";
    }
    echo $view_hidden;
}

Any help is greatly appreciated, I've been requesting support from the ACF guys but they are unable to help.

Thanks!

Share Improve this question edited Nov 29, 2017 at 19:50 Drupalizeme 1,6262 gold badges13 silver badges17 bronze badges asked Nov 29, 2017 at 17:57 Paul CullenPaul Cullen 1132 silver badges8 bronze badges 2
  • Are you referring to the page title in the <title> tag or the displayed page title on the page? Also don't see a return for the my_custom_title function. Those two questions will effect how you'd want to approach this. – josh.chavanne Commented Nov 29, 2017 at 18:20
  • If this is not for the HTML title tag then your better approach will be in your template to make this modification. – Drupalizeme Commented Nov 29, 2017 at 18:38
Add a comment  | 

2 Answers 2

Reset to default 2

If you refer to the post title you need to hook your function to the_title filter like explained in the codex.

add_filter('the_title', 'my_custom_title', 10, 2);

If you refer to the HTML meta in your page then you need to hook your function to document_title_parts filter explained here.

add_filter('document_title_parts', 'my_custom_title', 10, 2);

The two filters work differently as the first passes the title as a string and the second an array which contains the title as well. So depending on which one you need your code will need to be adapted to work accordingly.

If you can explain better your needs a better answer can be given.

In order to change the title, you need to use the_title filter, also if you want to determine if the user is logged in or not just use is_user_logged_in() function.

function my_custom_title( $title, $id = null ) {
    global $post;
    if( $post->post_type == 'sales_listings' && !is_user_logged_in() ) {
        $title =  "Confidential";
    } 
    return $title;
}
add_filter( 'the_title', 'my_custom_title', 10, 2 );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745626365a4636831.html

相关推荐

  • advanced custom fields - How to change the page title from functions.php

    I have an ACF custom field that depends on the value I need to change the page title, however, I cannot affect the page

    22天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信