wordpress添加媒体_如何添加内容并完全操纵WordPress RSS源

wordpress添加媒体

A while back we shared how you can control your WordPress RSS Footer with a use of a famous plugin called RSS Footer by Joost. While the plugin is great, but it is very limited. You can only add text to the footer, and it is always the same text displayed on each post’s footer. What if you want to display different text for each post in your RSS post? What if you want specific posts to have a different title in RSS Feeds? What if you want to display a specific custom field in your RSS Feeds? Well this is why we present you this article which will show you how to add any type of content in your WordPress RSS Feeds. This hack will put you in control of your WordPress RSS Feeds and you can manipulate it in anyway you want.

前一段时间,我们分享了如何使用著名的Joost的RSS Footer插件控制WordPress RSS Footer 。 虽然插件很棒,但是非常有限。 您只能将文本添加到页脚中,并且始终与每个帖子的页脚中显示的文本相同。 如果要在RSS帖子中为每个帖子显示不同的文本怎么办? 如果您希望特定帖子在RSS Feed中具有不同的标题怎么办? 如果要在RSS Feed中显示特定的自定义字段怎么办? 这就是为什么我们向您介绍本文的原因,它将向您展示如何在WordPress RSS Feed中添加任何类型的内容。 该技巧将使您可以控制WordPress RSS源,并且可以随时进行操作。

Note: This hack is not for beginners. Only users who feel comfortable with editing functions.php file and have some knowledge of php should try this. Beginner users should either use the plugin mentioned in the article above, or consult professionals like us to do it for you.

注意:此技巧不适用于初学者。 只有对编辑functions.php文件感到满意并且对php有所了解的用户才可以尝试此操作。 初学者应使用上面文章中提到的插件,或咨询像我们这样的专业人员来为您完成此任务。

1.将自定义字段添加到WordPress RSS页脚 (1. Add a Custom Field to your WordPress RSS Footer)

In this first example, we will show you how you can use custom field to display specific text/object in your WordPress RSS Feed. This trick will allow you to show different text, advertisement, image, or anything else for each post. First open your functions.php and paste the following codes in the php tags:

在第一个示例中,我们将向您展示如何使用自定义字段在WordPress RSS Feed中显示特定的文本/对象。 此技巧可让您为每个帖子显示不同的文本,广告,图像或其他内容。 首先打开您的functions.php并将以下代码粘贴到php标签中:


function wpbeginner_postrss($content) {
global $wp_query;
$postid = $wp_query->post->ID;
$coolcustom = get_post_meta($postid, 'coolcustom', true);
if(is_feed()) {
if($coolcustom !== '') {
$content = $content."<br /><br /><div>".$coolcustom."</div>
";
}
else {
$content = $content;
}
}
return $content;
}
add_filter('the_excerpt_rss', 'wpbeginner_postrss');
add_filter('the_content', 'wpbeginner_postrss');

Now most of you are wondering how this code is functioning. So here is an explanation. We create a function called wpbeginner_postrss which runs a global wp_query to search in each post if a custom field called “coolcustom” is defined. If Cool Custom is defined then it shows the value after the post content. If there is no custom field defined, then the function by default shows just the post content and nothing else. We use the variable $content to display content. We are using if(is_feed) function and adding the custom text or other content to the main post content itself which you can see by the second filter. But it will only be displayed in the RSS Feeds due to our user of if(is_feed) function. By doing it this way, we avoid all the compatibility issues.

现在,大多数人都在想这段代码是如何工作的。 所以这是一个解释。 我们创建了一个名为wpbeginner_postrss的函数,如果定义了一个名为“ coolcustom”的自定义字段,它将运行一个全局wp_query来搜索每个帖子。 如果定义了“酷自定义”,那么它将在帖子内容之后显示该值。 如果未定义自定义字段,则默认情况下该函数仅显示帖子内容,而仅显示其他内容。 我们使用变量$ content来显示内容。 我们正在使用if(is_feed)函数,并将自定义文本或其他内容添加到主要内容中,您可以通过第二个过滤器看到该内容。 但是由于我们使用if(is_feed)函数的用户,它只会显示在RSS Feed中。 通过这种方式,我们避免了所有兼容性问题。

Some of you will say but you just did what RSS footer plugin does with a function. Yes and NO. Yes we are adding the code at the footer of the post, but this text is not the same exact text for each post. It is different because you specify different text for each post via custom fields. This trick will be very handy to comply with the new FTC guidelines for blogs that have all different type of posts.

你们中有些人会说,但是您只是做了RSS页脚插件使用的功能。 是的,没有。 是的,我们将代码添加到帖子的页脚,但是此文本与每个帖子的确切文本都不相同。 这是不同的,因为您通过自定义字段为每个帖子指定了不同的文本。 遵循新的FTC指南(包含所有不同类型帖子的博客),此技巧非常方便。

2.在RSS中的标题中添加其他文本 (2. Adding Additional Text to Post Titles in RSS)

Does your blog have guest posts, sponsored posts, and review posts? Well if you do then you will find this very useful. Some bloggers have custom styling to display each different type of post, so their users can distinguish between them. But when these posts go in a reader, all the stylings are gone. That is when this trick comes in handy. In this trick we will add any text either before or after the title.

您的博客中是否有嘉宾帖子,赞助帖子和评论帖子? 好吧,如果您这样做,那么您会发现这非常有用。 一些博客作者具有自定义样式以显示每种不同类型的帖子,因此他们的用户可以区分它们。 但是,当这些帖子输入读者时,所有样式都消失了。 这就是这个技巧派上用场的时候。 在这个技巧中,我们将在标题之前或之后添加任何文本。

For example if your title was “Commercial WordPress Theme – StudioPress” and it was a sponsored post, then you can change it to “Sponsored Post: Commercial WordPress Theme – StudioPress”. Same if someone wrote a guest post etc.

例如,如果您的标题是“ Commercial WordPress Theme – StudioPress ”,并且它是赞助帖子,则可以将其更改为“ Sponsored Post:Commercial WordPress Theme – StudioPress”。 如果有人写来宾帖子等也是如此。

To accomplish this, open your functions.php file and add the following code in there:

为此,请打开您的functions.php文件,并在其中添加以下代码:


    function wpbeginner_titlerss($content) {
    global $wp_query;
    $postid = $wp_query->post->ID;
    $gpost = get_post_meta($postid, 'guest_post', true);
    $spost = get_post_meta($postid, 'sponsored_post', true);

    if($gpost !== '') {
    $content = 'Guest Post: '.$content;
    }
    elseif ($spost !== ''){
    $content = 'Sponsored Post: '.$content;
    }
    else {
    $content = $content;
    }
    return $content;
    }
    add_filter('the_title_rss', 'wpbeginner_titlerss');

Explanation for the code:

代码说明:

We are using the function called wpbeginner_titlerss which runs a global wp_query to search in each post if it contains either $gpost or $spost. These two elements are basically looking for two specific custom fields called “guest_post” or “sponsored_post”. If anyone has these custom fields added with a value true, then the code will add it in the text. If not then you will just see the normal title. You can see first the code looks for if $gpost is true, if that is not true whether $spost is true. If that is not also not defined, then it displays normal content. But if either one of them was true, then it displays the different text you specify here. We are using $content string to display the post title.

我们正在使用名为wpbeginner_titlerss的函数,该函数运行全局wp_query来搜索每个帖子中是否包含$ gpost或$ spost。 这两个元素基本上是在寻找两个称为“ guest_post”或“ sponsored_post”的自定义字段。 如果有人为这些自定义字段添加了值为true的代码,则代码会将其添加到文本中。 如果没有,那么您只会看到普通标题。 您首先可以看到,如果$ gpost为true,则代码将查找,如果$ spost为true,则为true。 如果还没有定义,则显示正常内容。 但是,如果其中任何一个为真,那么它将显示您在此处指定的不同文本。 我们使用$ content字符串显示帖子标题。

Now that was just displaying custom fields in the title. Do you want to display Category name on each title? Well then you should simply paste the following code in your functions.php file:

现在,仅在标题中显示自定义字段。 是否要在每个标题上显示类别名称? 好吧,您只需将以下代码粘贴到functions.php文件中:


function wpbeginner_cattitlerss($content) {
$postcat = "";
foreach((get_the_category()) as $cat) {
$postcat .= ' ('.$cat->cat_name . ')';
}
$content = $content.$postcat;
return $content;
}
add_filter('the_title_rss', 'wpbeginner_cattitlerss');

Explanation: We are using the function wpbgeinner_cattitlerss to get the category ID for each post and then displaying that right next to the title. So if the title is “Get Contact Form 7” now it would be “Get Contact Form 7 [Plugins]”. You can see that there is no if then variable in this code. We use $content for the main title and $postcat variable to define the category name. You rearrange that if you like.

说明:我们正在使用wpbgeinner_cattitlerss函数获取每个帖子的类别ID,然后在标题旁边显示该ID。 因此,如果标题现在为“获取联系表7”,则为“获取联系表7 [插件]”。 您可以看到此代码中没有if then变量。 我们使用$ content作为主要标题,并使用$ postcat变量定义类别名称。 如果愿意,可以重新排列。

3.在RSS中的所有帖子上添加相同的文本 (3. Add Same Text on all Posts in RSS)

If you just want to add the same text then you might as well use the plugin called RSS Footer by Joost because it is easier. But if you want to do it yourself this is how you do it. Open your functions.php file add the following code:

如果只想添加相同的文本,则最好使用Joost的RSS Footer插件,因为它更容易。 但是,如果您想自己做,这就是您的做法。 打开您的functions.php文件,添加以下代码:


function wpbeginner_postrss($content) {
if(is_feed()){
$content = 'This post was written by Syed Balkhi '.$content.'Check out WPBeginner';
}
return $content;
}
add_filter('the_excerpt_rss', 'wpbeginner_postrss');
add_filter('the_content', 'wpbeginner_postrss');

Explanation: We are calling a function wpbeginner_postrss to add in each post a content before the post saying This Post was written by Syed Balkhi and after the content Check out WPBeginner. But we add the function if(is_feed), so it will only be displayed in RSS Feeds.

说明:我们正在调用函数wpbeginner_postrss在每个帖子中添加一个内容,该帖子之前说Syed Balkhi撰写了此帖子,然后在该内容之后签出WPBeginner。 但是我们添加了if(is_feed)函数,因此它将仅显示在RSS Feed中。

This would be very helpful if you wanted to sell ads on specific posts in RSS, add custom FTC guidelines or just wanted to have more control over your RSS Feeds.

如果您想在RSS中的特定帖子上出售广告,添加自定义FTC指南或只是想对RSS Feed进行更多控制,这将非常有帮助。

Source: We used Joost’s RSS Footer plugin for a lot of guidance in writing this tutorial. The title RSS hack part we got from a French tutorial site and we added our own variables and gave it the ability to be custom titles per custom field.

来源:我们在编写本教程时使用了Joost的RSS Footer插件作为很多指导。 标题RSS hack部分是从法语教程站点获得的 ,我们添加了自己的变量,并使其能够成为每个自定义字段的自定义标题。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-content-and-completely-manipulate-your-wordpress-rss-feeds/

wordpress添加媒体

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值