X

Prepend ‘Sponsored post:’ to post title using post_meta

themify ultra demo setup

Simple little snippet that will use a custom field sponsored set to true will prepend "'Sponsored post:" to the_title. The second snippet will prepend the value of sponsored to the_title.


Code Snippet:


add_filter( 'the_title', 'wps_sponsored' );
function wps_sponsored( $title ) {
global $post;
$sponsored = get_post_meta($post->ID, 'sponsored', true);
if( is_single() && $sponsored == 'true' ){
return 'Sponsored post: '.$title;
}
return $title;
}



The second snippet will prepend whatever value you add to the sponsored custom field to the_title.


Code Snippet:


add_filter( 'the_title', 'wps_sponsored' );
function wps_sponsored( $title ) {
global $post;
$sponsored = get_post_meta($post->ID, 'sponsored', true);
if( is_single() && !empty($sponsored) ){
return $sponsored.' '.$title;
}
return $title;
}

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!