WordPress:Adding Post Feeds to the Header

来自站长百科
跳转至: 导航、​ 搜索

WordPress generates a unique RSS feed for each post on your blog, which can by default be accessed in the post metadata section below the post itself. However, in order to make the most of your feed you need a link element pointing to it, telling browsers, bots, and whatever and whoever else might be accessing your page that this is a feed, not just any old XML file.

WordPress为博客中的每篇文章产生一个唯一的RSS feed,默认情况下,在文章下面的文章metadata部分,能够访问这个RSS feed。然而,为了要创建大多数的feed,你需要一个link指向feed,通知浏览器,bots以及任何可能访问你的网页的人或者器件,这是个feed,而不只是任何旧的XML文件。

Unfortunately WordPress doesn't provide a dedicated option for doing this yet, so we're going to have to make one ourselves. This tutorial will provide code you can add to your header, and explain how the different parts work. If you aren't interested in what's going on and just want the solution, skip down to the [[WordPress:#The final code|The final code]] section!

不好的是,WordPress没有提供一个专门的选项来执行这一任务,因为我们决定自己创建一个选项。这个指南会向你提供代码,你可以将这个代码添加到你的标头上,并且解释不同运行是怎样运行的。如果你对目前运行什么不感兴趣,指示想要解决办法,你可以跳到[[WordPress:#The final code|最后的编码]] 部分!

The problem[ ]

问题[ ]

We must create a Link element in our header that points readers to our feed. It must change to reflect the page being viewed, and must not be generated if the page is not a post. To hook into the post system we must make use of WordPress:The Loop to get the relevant data from the database.

我们必须在标头中创建一个链接,向用户指向我们的feed。Feed必须更改来反应正在得到访问的网页,如果网页不是在一篇文章上,就不能够产生。要hook into文章系统,我们必须使用The Loop从数据库中得到相关的数据。

The logical solution to the problem would be to examine the post metadata code and copy over the command it uses to generate its link. Unfortunately, the output of comments_rss_link() is an HTML 'anchor' link (a href), which isn't what we want at all. It is designed for linking page elements, but we want to add information that isn't tied to any particular part of the page.

解决这个问题的合理方法是检查文章的metadata代码并且不止文章产生链接的代码。不利的是,comments_rss_link()输出的是HTML 'anchor'链接(a href),这并不是我们想要的。这个链接是用来链接网页内容的,但是我们想要添加的信息,不会成为网页的某个部分。

Link elements[ ]

链接 elements[ ]

A Link element is included in a page header, and links to some form of off-page resource. Have a look at your existing header for some examples. For RSS feeds, they take the following basic format:

链接 element包含在网页的标头中,而且链接到一些off-page形式的资源。插件你当期的标头中的一些例子。RSS feeds,拥有以下的一些基本的格式:

<link rel="alternate" type="application/rss+xml" title="Feed Title" href="Feed Location" /> <link rel="alternate" type="application/rss+xml" title="Feed Title" href="Feed Location" />


The rel element tells the reader what this link is providing. There are many values accepted by rel: ours, alternate, indicates that the link points to an alternative form of the current page, its feed. We might also use alternate to signify a version of the page in a different language, or many other things. The type of alternate page being provided is of course defined in type, which should be self explanatory. Next is the title, which performs the same function as title elsewhere on the web; displaying a friendly name instead of the resource's location. Finally, href is the location of the link's target on the internet, its URL. We then close the tag.

rel element 告诉了读者这个链接提供了什么内容。 rel接受许多参数值: 我们的, 更改的,暗示链接 指向当前网页的另一种形式,网页的feed。我们也可能使用另一种方法指明网页的版本是使用另一种语言编写的,或者指明许多其它的内容。提供的另一种网页的样式当然是在type中定义的,而且是清楚明了的。下一步是标题,作用与网站上其它地方的标题 作用相同;显示了友好的名称而不是资源的位置。最后, href是因特网上的链接的位置,链接的 URL。然后我们关闭标签。

The variables[ ]

变数[ ]

We can't just paste the above XHTML code in of course, unless you only ever have one post on your entire blog. We must make the values reflect the page they are being placed on, and in PHP this is done through a system of substitution. We enter PHP code in and when the page is generated WordPress replaces it with the data we asked for.

当然我们不能只粘贴以上的XHTML代码,除非你整个博客上只有一篇文章。我们必须使得参数值反映参数值所在的网页,而且在PHP中,这个操作是通过提交系统来完成的。我们输入PHP代码,而且网页生成之后,WordPress用我们需求的数据来替代PHP代码。

We will need to substitute three pieces of data:

我们需要替换三块数据:

  • The feed title
  • The location of the blog
  • The ID of the current post

And to do this we need the following variables:

  • the_title()
  • bloginfo('url')
  • the_ID()


  • feed 标题
  • 博客地址
  • 当前文章的ID

And to do this we need the following variables: 执行这个操作,我们需要下面的变数:

  • the_title()
  • bloginfo('url')
  • the_ID()


We add the variables where we want their result to be inserted, and WordPress does the rest. It isn't quite that simple though, as we have to tell WordPress that we want the commands interpreted as such and not simply printed out as text. We do this by wrapping <?php and ?> around the variables. We must also, with a few exceptions, add a semicolon after each line for programming reasons that I won't go into.

我们在想要插入变数结果的位置,添加变数,WordPress执行其它的操作。操作并不是非常简单,因为我们需要指示WordPress需要将命令翻译,而不是直接作为文本列出。通过在变数周围添加<?php and ?>,我们就能够使得WordPress翻译命令。同时,除了一些例外之外,因为编程方面的原因,我们需要在每一行的后面添加个分号。

Substituting those variables results in: 在下面命令中,替换这些变数: <link rel="alternate" type="application/rss+xml" title="<?php the_title(); ?> Comments" href="<?php bloginfo('url'); ?>/?feed=rss2&amp;p=<?php the_ID(); ?>" />

<link rel="alternate" type="application/rss+xml" title="<?php the_title(); ?> Comments" href="<?php bloginfo('url'); ?>/?feed=rss2&amp;p=<?php the_ID(); ?>" />

Note that we have replaced the href's '&' with &amp;. The ampersand is not a legal character in XHTML, and &amp; is the 'encoded' version that has the same effect but does not cause browsers trouble.

注意,我们已经用&amp;替换了href's '&'。&并不是XHML中的合法的字符,而且&amp;是'编码后'的版本,会有一些影响,但是不会产生浏览器方面的问题。

The Loop[ ]

The Loop[ ]

So we've got our link, and it has the key fields replaced with code that will fill them automatically. But we can't just use the variables anywhere. They must be within 'WordPress:The Loop'. Read more about The Loop at this link. If you've come back completely blasted - don't worry. It isn't nearly as complex as the article might make out.

我们得到了链接,将key fields替换为代码,代码会自动地填进。但是我们不能够在任何位置使用变数。我们必须在'The Loop'内使用变数。在这个链接上阅读关于Loop的内容。如果你沮丧地返回了-不要担心。这个操作并不完全像文章中所描述地那样简单。

Ideally we would add our code to the existing Loop, the one that already provides our posts, comments, and whatnot. While that would work in some browsers, it is completely the wrong place for what we are trying to do. The default Loop is entirely within the body of the page, and link elements belong in the head. It would be very bad practice to add the code to the body, and cause all sorts of problems.

理想话地,我们会将代码添加到当前的Loop,Loop已经提供我们的文章,评论,以及其它内容。虽然这在同一个浏览器中能够运行,但是这并不是我们试图操作的位置。默认Loop完全在网页主体之内,而且链接在标头。将代码添加到网页主体非常不好,会导致许多问题。

So we need a new Loop. Fret not, it's easy. 因此我们需要一个新的Loop。不要烦恼,这很简单: <?php while (have_posts()) : the_post(); ?>

<?php endwhile; ?>

<?php while (have_posts()) : the_post(); ?>

<?php endwhile; ?>

There you have it, a secondary Loop. Stick our code between those two lines and it will work just fine. 在第二个Loop中,你可以添加Loop。将我们的代码粘贴在这两行之间,就能够运行了。

There's only one problem. Because of the way The Loop works, if we leave it at that we will have the right feed in the document head, but the post displayed in the body of the page, the thing you actually read, will be the next one in the database. This won't do at all. Luckily the fix is once again simple:

这里只有一个问题。因为Loop的运行的方式不同,如果我们保留那种方式,在文件标头我们会有合适的feed,但是文章在网页的主题中显示,你真正阅读的内容,是数据库中的下一篇文章。这行不通。幸运地是,解决方法仍然很简单:

<?php rewind_posts(); ?>

Our loop is now complete.

<?php rewind_posts(); ?>

现在我们的loop完成了。

Conditional Tags ('if statements')[ ]

条件式标签 ('if 声明')[ ]

We are almost done. We have our link element, we are substituting data in to it, and it is able to hook into the database without messing anything up. But if you add the code in its current state, it will be executed on every page on the blog, even when there isn't a post present. This probably won't do any harm per se, but it will be very confusing for users. We need to tell WordPress that we only want our link to be added under a certain circumstance: the page being a post. This is done with a 'Conditional Tag', or as a programmer might know it an 'if statement'.

我们几乎完成了,我们拥有链接element,我们向链接element替换数据,而且数据能够hook into数据库,不会将数据库混乱。但是如果你添加当前状态的代码,代码就会在博客中的每个网页上执行,即使目前博客上没有文章,也会出现这样的情况。这本身可能不会产生什么坏的影响,但是这会使用户感到疑惑。我们需要指示WordPress,只有在某些条件下,我们才会添加链接:网页是篇文章的时候。这个需要'条件式标签'来操作,或者作为参数可能识别这个为'if 声明'。

<?php if (is_single()) { ?>

<?php } ?>

<?php if (is_single()) { ?>

<?php } ?>

Anything between those two lines will only execute is is_single() is true; if the page is a post. Now we are all set!

这两行之间的任何内容,只有is_single()是正确的,才会执行;如果网页是篇文章。现在我们都设置好了!

The final code[ ]

<?php if (is_single()) { ?>

<?php while (have_posts()) : the_post(); ?>

<link rel="alternate" type="application/rss+xml" title="<?php the_title(); ?> Comments" href="<?php bloginfo('url'); ?>/?feed=rss2&amp;p=<?php the_ID(); ?>" />

<?php endwhile; ?>

<?php rewind_posts(); ?>

<?php } ?>


最终的代码[ ]

<?php if (is_single()) { ?>

<?php while (have_posts()) : the_post(); ?>

<link rel="alternate" type="application/rss+xml" title="<?php the_title(); ?> Comments" href="<?php bloginfo('url'); ?>/?feed=rss2&amp;p=<?php the_ID(); ?>" />

<?php endwhile; ?>

<?php rewind_posts(); ?>

<?php } ?>

Congratulations! Paste this code into header.php (preferably next to the existing site feed, for readability) and each post on your blog will properly notify browsers, bots and users of its RSS feed. Certainly better than an obscure little link in the metadata. For a quick demo, view a post in Firefox and you will get two options whenever you click the Live Bookmark icon in the Address Bar. If you don't, you've made a mistake somewhere and need to try again. Viewing your page source might make the error easier to find.

祝贺你!将这个代码粘贴到header.php (更适宜添加到当前站点的feed,更容易阅读),而且你的博客上的每篇文章会适当地通知浏览器,bots以及RSS feed的用户。当然比metadata中模糊的链接要好。对于一个快速的demo,在Firefox中访问一篇文章,当你点击地址栏中的Live Bookmark的时候,你就会得到两个选项。如果你没有得到两个选项,你可能在那里出错了,需要再试一次。访问网页的源代码,可能更容易找到问题所在。