Family Encyclopedia >> Electronics

How to add a sponsored post prefix to post a title in WordPress

You often see bloggers post sponsored posts on their blog. Recently, one of our users asked if it was possible to automatically add a "Sponsored" prefix to post titles. In this article, we will show you how to add sponsored post prefix to post title in WordPress.

How to add a sponsored post prefix to post a title in WordPress

Add sponsored post prefix using custom fields

Custom fields allow you to add meta information to your posts. In this article we will use custom fields to add a sponsored meta field to a post.

First you need to create a new post or edit an existing one. In the post editor, scroll down to the custom fields metabox. If you can't see the custom fields metadata box in your post editing area, you need to click Screen Options Button in the upper right corner of the screen. This will bring up a menu where you need to check the box next to custom field options.

How to add a sponsored post prefix to post a title in WordPress

Now scroll down to the custom fields meta box and add patrocinado in the custom field Name , and cierto in the value countryside.

How to add a sponsored post prefix to post a title in WordPress

Next, you need to save your post and scroll down to the custom fields box. You'll notice that the custom field's meta box now displays a dropdown menu. The next time you need to set a sponsored post, all you need to do is select sponsored from the dropdown and enter true in the value field.

After adding the sponsored custom field to your post, you need to copy and paste this code snippet into your theme's functions.php file or a site-specific plugin.

 add_filter ('the_title', 'wpb_sponsored'); función wpb_sponsored ($ title) global $ post; $ sponsors_text = 'Publicaciones patrocinadas'; $ patrocinado = get_post_meta ($ post-> ID, 'patrocinado', verdadero); if ($ sponsors == 'true' && in_the_loop ()) return $ sponsor_text. $ title; devolver $ título; 

That is all. Try visiting the post you previously edited, and you'll see Sponsored Post:prefixed with the post title.

If you study the code snippet, you'll notice that we've wrapped a sponsored post text around a CSS class. Using this CSS class you can highlight the text within the post title. Here's some CSS you can add to your child theme or style sheet.

 .texto_autorizado fondo: #eeffee; tamaño de letra: pequeño; transformación de texto: mayúsculas; relleno: 5px; 

Feel free to modify the CSS to suit your needs.

Adding sponsored post suffix to post title in WordPress

In case you want to display the sponsored post text after the post title, you can achieve this using this code snippet:

 add_filter ('the_title', 'wpb_sponsored'); función wpb_sponsored ($ title) global $ post; $ sponsors_text = 'Publicaciones patrocinadas'; $ patrocinado = get_post_meta ($ post-> ID, 'patrocinado', verdadero); if ($ sponsors == 'true' && in_the_loop ()) return $ title. $ spons_text; devolver $ título; 

If you study the code we have only made two changes. We've added a single letter space before the sponsored text, and then changed the order to display $ titulo first.

That's it, we hope this article helped you to add sponsored post prefix/suffix to post title in WordPress.

If you enjoyed this article, please subscribe to our WordPress YouTube Channel video tutorials. You can also find us on Twitter and Google.+.