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中为帖子标题添加赞助的帖子前缀。

使用自定义字段添加赞助的后缀 (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.

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

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

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标题设置

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

  1. wordpress默认密码_如何在WordPress中为新用户设置默认管理员配色方案

    wordpress默认密码 One of the most talked about feature of WordPress 3.8 is the new admin interface. It i ...

  2. wordpress谷歌字体_如何在WordPress中添加Google Maps

    wordpress谷歌字体 You can add all kind of content into your WordPress site. At WPBeginner we have shown ...

  3. wordpress图像大小_如何在WordPress中添加图像积分(逐步操作)

    wordpress图像大小 Recently, some of our readers asked us about how to properly add image credits in Word ...

  4. wordpress自动发布_如何在WordPress中跟踪发布想法

    wordpress自动发布 Are you looking for a way to keep track of your post ideas in WordPress? Anyone who ha ...

  5. wordpress谷歌字体_如何在WordPress帖子编辑器中添加Google字体

    wordpress谷歌字体 Fonts are one of the most important aspects of your WordPress blog, and in case you di ...

  6. wordpress撰写模式_如何在WordPress中撰写好的博客评论并绕过垃圾邮件过滤器

    wordpress撰写模式 Have you ever thought about what makes a good blog comment? Want to know of a sure-fir ...

  7. wordpress标签云_如何在WordPress中建立更好的标签云

    wordpress标签云 Once you've defined a great set of tags for your WordPress posts (or pages), you'll wan ...

  8. wordpress 数据库_如何在WordPress中创建视频库(逐步)

    wordpress 数据库 Recently, a user asked us how to create a video gallery in WordPress. WordPress makes ...

  9. wordpress添加媒体_如何在WordPress中添加jQuery FAQ手风琴

    wordpress添加媒体 Recently one of our users asked us if there was a way for them to add a FAQ accordion ...

最新文章

  1. 为Nagios定义自定义脚本检测内存状态
  2. 华为上机考试题系列(二):HJ107求解立方根
  3. NoSQL最新现状和趋势:云NoSQL数据库将成重要增长引擎
  4. 如何在 Serverless K8s 集群中低成本运行 Spark 数据计算?
  5. 51CTO下载中心资源上传协议
  6. KVM详解(一)——KVM基础知识
  7. 线性表——顺序表——时间复杂度计算
  8. “小马激活”病毒新变种分析报告
  9. 如何去掉input type=file中的选择文件
  10. c语言计算标准体重作业,c语言/* 已知成人标准体重粗算公式:
  11. Redhat_rhel8.0_FTP服务配置
  12. 5 款开源的 Linux 策略模拟游戏 | Linux 中国
  13. JavaScript的调试工具
  14. 计算机基础作业题及答案,计算机文化基础习题集(含答案)
  15. Windows系统下的部分dos命令
  16. a标签 :focus的使用(:focus)
  17. 依照测试用例分类(按功能)的结果生成对应的universe文件
  18. 菜鸡做题·20200418会员登陆页面(CTF)
  19. 计组学习笔记2(RISC v版)
  20. 购买老域名,利用桔子seo来查找域名的历史快照信息

热门文章

  1. QtCreator不能加载qrc里面的资源
  2. matlab软件有著作权,软件著作权matlab样例
  3. linux亚马逊服务器-安装redis
  4. 怎样裁剪gif尺寸?教你三步在线裁剪动图
  5. Julia学习笔记:堆积柱形图的绘制
  6. QT编写简易截图工具
  7. golang gin binding.JSON undefined
  8. 政采云测试工程师校招面经
  9. Java heap space解决
  10. HDU4417 Super Mario(离线树状数组或者主席树+二分)