![](https://i-blog.csdnimg.cn/blog_migrate/2eee415dad51f9305ba3445c6b67fa47.png)
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 Sponsored Post prefix added to post title in WordPress](https://i-blog.csdnimg.cn/blog_migrate/c00c11c74e4b7ba1ec2b40a6c1e959ef.png)
使用自定义字段添加赞助的后缀 (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 Making custom fields meta box visible in WordPress post editor](https://i-blog.csdnimg.cn/blog_migrate/5dc2872e1f2536da4f432a6eda13d02b.png)
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 Adding sponsored custom field in the custom fields meta box](https://i-blog.csdnimg.cn/blog_migrate/0c578f5cf255d4df60f4601e2b556028.png)
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视频教程。 您也可以在Twitter和Google+上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-sponsored-post-prefix-to-post-title-in-wordpress/
wordpress标题设置