站长百科 | 数字化技能提升教程 数字化时代生存宝典
首页
数字化百科
电子书
建站程序
开发
服务器
办公软件
开发教程
服务器教程
软件使用教程
运营教程
热门电子书
WordPress教程
宝塔面板教程
CSS教程
Shopify教程
导航
程序频道
推广频道
网赚频道
人物频道
网站程序
网页制作
云计算
服务器
CMS
论坛
网店
虚拟主机
cPanel
网址导航
WIKI使用导航
WIKI首页
最新资讯
网站程序
站长人物
页面分类
使用帮助
编辑测试
创建条目
网站地图
站长百科导航
站长百科
主机侦探
IDCtalk云说
跨境电商导航
WordPress啦
站长专题
网站推广
网站程序
网站赚钱
虚拟主机
cPanel
网址导航专题
云计算
微博营销
虚拟主机管理系统
开放平台
WIKI程序与应用
美国十大主机
编辑“
WordPress:Adding Post Feeds to the Header
”
人物百科
|
营销百科
|
网赚百科
|
站长工具
|
网站程序
|
域名主机
|
互联网公司
|
分类索引
跳转至:
导航
、
搜索
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
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 [http://www.w3.org/TR/REC-html40/struct/links.html#h-12.3 <tt>link</tt>] 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,你需要一个[http://www.w3.org/TR/REC-html40/struct/links.html#h-12.3 <tt>link</tt>]指向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 <tt>Link</tt> 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. 我们必须在标头中创建一个<tt>链接</tt>,向用户指向我们的feed。Feed必须更改来反应正在得到访问的网页,如果网页不是在一篇文章上,就不能够产生。要hook into文章系统,我们必须使用[[WordPress:The Loop|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 <tt>comments_rss_link()</tt> is an HTML 'anchor' link (<tt>a href</tt>), 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代码并且不止文章产生链接的代码。不利的是,<tt>comments_rss_link()</tt>输出的是HTML 'anchor'链接(<tt>a href</tt>),这并不是我们想要的。这个链接是用来链接网页内容的,但是我们想要添加的信息,不会成为网页的某个部分。 ==Link elements== ==链接 elements== A <tt>Link</tt> 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: <tt>链接</tt> element包含在网页的标头中,而且链接到一些off-page形式的资源。插件你当期的标头中的一些例子。RSS feeds,拥有以下的一些基本的格式: <tt><link rel="alternate" type="application/rss+xml" title="Feed Title" href="Feed Location" /></tt> <tt><link rel="alternate" type="application/rss+xml" title="Feed Title" href="Feed Location" /></tt> The <tt>rel</tt> element tells the reader what this link is providing. There are many values accepted by <tt>rel</tt>: ours, <tt>alternate</tt>, indicates that the <tt>link</tt> 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 <tt>type</tt>, which should be self explanatory. Next is the title, which performs the same function as <tt>title</tt> elsewhere on the web; displaying a friendly name instead of the resource's location. Finally, <tt>href</tt> is the location of the <tt>link</tt>'s target on the internet, its URL. We then close the tag. <tt>rel</tt> element 告诉了读者这个链接提供了什么内容。 <tt>rel</tt>接受许多参数值: 我们的, <tt>更改的</tt>,暗示<tt>链接</tt> 指向当前网页的另一种形式,网页的feed。我们也可能使用另一种方法指明网页的版本是使用另一种语言编写的,或者指明许多其它的内容。提供的另一种网页的样式当然是在<tt>type</tt>中定义的,而且是清楚明了的。下一步是标题,作用与网站上其它地方的<tt>标题</tt> 作用相同;显示了友好的名称而不是资源的位置。最后, <tt>href</tt>是因特网上的<tt>链接</tt>的位置,链接的 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: *<tt>the_title()</tt> *<tt>bloginfo('url')</tt> *<tt>the_ID()</tt> * feed 标题 *博客地址 *当前文章的ID And to do this we need the following variables: 执行这个操作,我们需要下面的变数: *<tt>the_title()</tt> *<tt>bloginfo('url')</tt> *<tt>the_ID()</tt> 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 <tt><?php</tt> and <tt>?></tt> 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需要将命令翻译,而不是直接作为文本列出。通过在变数周围添加<tt><?php</tt> and <tt>?></tt>,我们就能够使得WordPress翻译命令。同时,除了一些例外之外,因为编程方面的原因,我们需要在每一行的后面添加个分号。 Substituting those variables results in: 在下面命令中,替换这些变数: <tt><link rel="alternate" type="application/rss+xml" title="'''<?php the_title(); ?>''' Comments" href="'''<?php bloginfo('url'); ?>'''/?feed=rss2&amp;p='''<?php the_ID(); ?>'''" /></tt> <tt><link rel="alternate" type="application/rss+xml" title="'''<?php the_title(); ?>''' Comments" href="'''<?php bloginfo('url'); ?>'''/?feed=rss2&amp;p='''<?php the_ID(); ?>'''" /></tt> Note that we have replaced the <tt>href</tt>'s '&' with <tt>&amp;</tt>. The ampersand is not a legal character in XHTML, and <tt>&amp;</tt> is the 'encoded' version that has the same effect but does not cause browsers trouble. 注意,我们已经用<tt>&amp;</tt>替换了<tt>href</tt>'s '&'。&并不是XHML中的合法的字符,而且<tt>&amp;</tt>是'编码后'的版本,会有一些影响,但是不会产生浏览器方面的问题。 ==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 [[WordPress:The Loop|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替换为代码,代码会自动地填进。但是我们不能够在任何位置使用变数。我们必须在'[[WordPress:The Loop|The Loop]]'内使用变数。在[[WordPress: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 <tt>link</tt> 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完全在网页主体之内,而且<tt>链接</tt>在标头。将代码添加到网页主体非常不好,会导致许多问题。 So we need a new Loop. Fret not, it's easy. 因此我们需要一个新的Loop。不要烦恼,这很简单: <tt><?php while (have_posts()) : the_post(); ?> <?php endwhile; ?></tt> <tt><?php while (have_posts()) : the_post(); ?> <?php endwhile; ?></tt> 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,但是文章在网页的主题中显示,你真正阅读的内容,是数据库中的下一篇文章。这行不通。幸运地是,解决方法仍然很简单: <tt><?php rewind_posts(); ?></tt> Our loop is now complete. <tt><?php rewind_posts(); ?></tt> 现在我们的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 <tt>link</tt> 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,只有在某些条件下,我们才会添加<tt>链接</tt>:网页是篇文章的时候。这个需要'条件式标签'来操作,或者作为参数可能识别这个为'if 声明'。 <tt><?php if (is_single()) { ?> <?php } ?></tt> <tt><?php if (is_single()) { ?> <?php } ?></tt> Anything between those two lines will only execute is <tt>is_single()</tt> is true; if the page is a post. Now we are all set! 这两行之间的任何内容,只有<tt>is_single()</tt>是正确的,才会执行;如果网页是篇文章。现在我们都设置好了! ==The final code== <tt><?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 } ?></tt> ==最终的代码== <tt><?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 } ?></tt> Congratulations! Paste this code into <tt>header.php</tt> (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. 祝贺你!将这个代码粘贴到<tt>header.php</tt> (更适宜添加到当前站点的feed,更容易阅读),而且你的博客上的每篇文章会适当地通知浏览器,bots以及RSS feed的用户。当然比metadata中模糊的链接要好。对于一个快速的demo,在Firefox中访问一篇文章,当你点击地址栏中的Live Bookmark的时候,你就会得到两个选项。如果你没有得到两个选项,你可能在那里出错了,需要再试一次。访问网页的源代码,可能更容易找到问题所在。
摘要:
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅
Wordpress-mediawiki:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)