Saturday, November 28, 2020
Home Blogging How to Add Sponsored Post Prefix to Post Title in WordPress

How to Add Sponsored Post Prefix to Post Title in WordPress

Typically you see bloggers publish sponsored posts on their weblog. Not too long ago one among our customers requested if it was potential to routinely add a “Sponsored” prefix in  titles. On this article, we’ll present you add sponsored post  prefix to  title in WordPress.

Including Sponsored Post Prefix Utilizing Customized Fields

Customized fields can help you add meta data to your posts. On this article we will probably be utilizing customized fields so as to add sponsored meta discipline to a post.

First you must create a brand new post or edit an current one. Within the post editor, scroll right down to the customized fields metabox. If you can’t see the customized fields meta field in your post edit space, then you must click on on the Display screen Choices button on the highest proper nook of the display screen. This may open a menu the place you must examine the field subsequent to customized fields choices.

Now scroll right down to the customized fields meta field and add sponsored within the customized discipline Title, and true within the worth discipline.

Subsequent you must save your post and scroll right down to customized fields meta field. You’ll discover that customized discipline meta field is now exhibiting a drop down menu. Subsequent time you must set a sponsored post, all you must do is choose sponsored from the drop down menu and enter true within the worth discipline.

After including the sponsored customized discipline to your post, you must copy and paste this code snippet into your theme’s capabilities.php file or a site-specific plugin.

1
2
3
4
5
6
7
8
9
10
add_filter( 'the_title', 'wpb_sponsored' );
function wpb_sponsored( $title ) {
   global $post;
   $sponsored_text = '<span class="sponsored_text"> Sponsored Post</span> ';
   $sponsored = get_post_meta($post->ID, 'sponsored', true);
   if( $sponsored == 'true' && in_the_loop() ){
       return $sponsored_text.$title;
   }
   return $title;
}

That’s all. Strive visiting the post you edited earlier, and you will note Sponsored post : prefix with the post title.

If you happen to examine the code snippet, you’ll discover that we’ve got wrapped sponsored post textual content roundCSS class. Utilizing this CSS class you may spotlight the textual content contained in the post title. Here’s a little CSS which you could add to your theme or baby theme‘s stylesheet.

1
2
3
4
5
6
.sponsored_text {
background: #eeffee;
font-size:small;
text-transform: uppercase;
padding:5px;
}

Be at liberty to change the CSS to fulfill your wants.

Including Sponsored Post Suffix to Post Title in WordPress

In case you need to show the sponsored post textual content after the post title, then you may obtain this through the use of this code snippet:

1
2
3
4
5
6
7
8
9
10
add_filter( 'the_title', 'wpb_sponsored' );
function wpb_sponsored( $title ) {
   global $post;
   $sponsored_text = '<span class="sponsored_text"> Sponsored Post</span> ';
   $sponsored = get_post_meta($post->ID, 'sponsored', true);
   if( $sponsored == 'true' && in_the_loop() ){
       return $title.$sponsored_text;
   }
   return $title;
}

If you happen to examine the code we’ve got made simply two modifications. Now we have added a single letter house earlier than the sponsored textual content, after which we’ve got switched the order to show $title first.

You May Also Like : 10 Most Wanted Category Hacks and Plugins for WordPress

That’s all, we hope this text helped you add a sponsored post prefix / suffix to post title in WordPress.

If you liked this article, make sure to hit the bell for getting our latest updates and share this article with your friends in every social media platforms.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Top 5 Conventional Professional Courses in India

Millions of scholars throughout India stand at crossroads of the profession upon completion of secondary and better secondary schooling. They share a standard dilemma:...

Top 25 Data Entry Jobs from Home Online/Offline: No Investment

Data Entry jobs are actually nice for individuals who need to work from dwelling however not a lot certified. Whether you're a housewife, a...

A Guide to BBA Course: Subjects, Syllabus, Colleges in India & More

Questions about larger schooling usually put college students in dilemma. Concerns over what programs to take put up larger secondary schooling are widespread amongst...

Top YouTubers in India & Most Subscribed YouTube Channels)

This digital period is a boon for the inventive individuals. In earlier years, inventive individuals, those that can dance, sing, mimic or cook dinner,...

Recent Comments