wordpress标题设置_如何在WordPress中的帖子标题中添加赞助的帖子前缀

wordpress标题设置

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

通常,您会看到博客作者在其博客上发布赞助帖子。 最近,我们的一位用户询问是否可以在帖子标题中自动添加“赞助商”前缀。 在本文中,我们将向您展示如何在WordPress中为帖子标题添加赞助的帖子前缀。

Sponsored Post prefix added to post title in WordPress
使用自定义字段添加赞助的后缀 (Adding Sponsored Post Prefix Using Custom Fields)

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

自定义字段允许您将元信息添加到您的帖子中。 在本文中,我们将使用自定义字段将赞助的meta字段添加到帖子中。

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 not see the custom fields meta box in your post edit area, then you need to click on the Screen Options button on the top right corner of the screen. This will open a menu where you need to check the box next to custom fields options.

首先,您需要创建一个新帖子或编辑一个现有的帖子。 在帖子编辑器中,向下滚动到“自定义字段”元框。 如果您在帖子编辑区域中看不到自定义字段元框,则需要单击屏幕右上角的“屏幕选项”按钮。 这将打开一个菜单,您需要在其中选中自定义字段选项旁边的框。

Making custom fields meta box visible in WordPress post editor

Now scroll down to the custom fields meta box and add sponsored in the custom field Name, and true in the value field.

现在向下滚动到自定义字段元框,并在自定义字段Name中添加sponsored ,在value字段中添加true

Adding sponsored custom field in the custom fields meta box

Next you need to save your post and scroll down to custom fields meta box. You will notice that custom field meta box is now showing a drop down menu. Next time you need to set a sponsored post, all you need to do is select sponsored from the drop down menu and enter true in the value field.

接下来,您需要保存您的帖子并向下滚动到“自定义字段”元框。 您会注意到自定义字段元框现在显示一个下拉菜单。 下次您需要设置赞助者帖子时,只需从下拉菜单中选择赞助者,然后在值字段中输入true。

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.

将赞助的自定义字段添加到帖子后,您需要将此代码段复制并粘贴到主题的functions.php文件或特定站点的插件中


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. Try visiting the post you edited earlier, and you will see Sponsored Post: prefix with the post title.

就这样。 尝试访问您先前编辑的帖子,您将看到Sponsored Post:带有标题的前缀。

If you study the code snippet, you will notice that we have wrapped sponsored post text around a CSS class. Using this CSS class you can highlight the text inside the post title. Here is a little CSS that you can add to your theme or child theme‘s stylesheet.

如果您研究代码片段,您会注意到我们已经将赞助的帖子文本包裹在CSS类周围。 使用此CSS类,您可以突出显示帖子标题内的文本。 这里有一些CSS,您可以将其添加到主题或子主题的样式表中。


.sponsored_text { 
background: #eeffee;
font-size:small;
text-transform: uppercase;
padding:5px;
}

Feel free to modify the CSS to meet your needs.

随意修改CSS以满足您的需求。

在WordPress标题中添加赞助后缀 (Adding Sponsored Post Suffix to Post Title in WordPress)

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

如果您想在帖子标题后显示赞助的帖子文本,则可以使用以下代码片段实现此目的:


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 study the code we have made just two changes. We have added a single letter space before the sponsored text, and then we have switched the order to display $title first.

如果您学习该代码,我们只需进行两项更改。 我们在赞助文本之前添加了一个字母空格,然后将顺序切换为先显示$title

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

仅此而已,我们希望本文能帮助您添加赞助的帖子前缀/后缀以在WordPress中发布标题。

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

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterGoogle+上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-sponsored-post-prefix-to-post-title-in-wordpress/

wordpress标题设置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值