admin管理员组

文章数量:1125785

I need to show a custom Field in a title (CPT) I have this code

function wpse_224340_document_title($title){
    global $post; // make sure the post object is available to us
    if(is_singular('mec-events')){ // check we're on a single post
        $release_author = get_post_meta(get_the_ID(), 'mec_fields_2', true);
        if($release_author != ""){ $title["title"].= " (" . $release_author. ")"; }
    }
    return $title;
}

But it doesn´t work, someone can help me with my problem?

I need to show a custom Field in a title (CPT) I have this code

function wpse_224340_document_title($title){
    global $post; // make sure the post object is available to us
    if(is_singular('mec-events')){ // check we're on a single post
        $release_author = get_post_meta(get_the_ID(), 'mec_fields_2', true);
        if($release_author != ""){ $title["title"].= " (" . $release_author. ")"; }
    }
    return $title;
}

But it doesn´t work, someone can help me with my problem?

Share Improve this question edited Feb 5, 2024 at 13:50 shanebp 5,0836 gold badges27 silver badges40 bronze badges asked Feb 5, 2024 at 11:03 LaurapnunezLaurapnunez 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

Use the wp_title filter, which passes the current title, separator, and location as arguments. It then appends the custom field value to the title if the conditions are meet. For more details :- Visit Link wp_title

function wpse_224340_document_title($title, $sep, $seplocation) {
    global $post;

    // make sure the post object is available to us
    if (is_singular('mec-events')) { // check we're on a single post
        $release_author = get_post_meta($post->ID, 'mec_fields_2', true);

        if ($release_author !== "") {
            $title .= " (" . $release_author . ")";
        }
    }

    return $title;
}

add_filter('wp_title', 'wpse_224340_document_title', 10, 3);

本文标签: