扩展默认的WordPress RSS Feed

WBOY
发布: 2023-09-04 14:05:04
原创
1306人浏览过

有时,您可能需要通过在网站外提交内容来增强您的在线形象并覆盖更广泛的受众。例如,您可能希望在最流行的社交网络聚合器上提供您的帖子,或者在移动设备上提供它们,或者在数字商店上发布您的音频/视频播客。

在大多数情况下,有必要通过添加自定义元数据来自定义 RSS Feed,使其适合发布。

在本教程中,我们将了解如何在两个主要平台上实现此目标:Flipboard 和 iTunes Store,但代码可以轻松针对其他平台和 Web 服务进行自定义。


简介

Flipboard 是一款适用于 Android 和 iOS 设备的社交网络聚合应用程序,它会定期从您的网站获取内容并以杂志格式呈现,以便移动用户可以通过智能手机或平板电脑上安装的应用程序阅读您的新闻。 iTunes Store 是一个在线数字媒体商店,您可以在其中发布音频或视频播客。

这两项服务的订阅都是免费的,但需要经过批准,特别是 Flipboard 似乎只接受拥有大量读者的网站。

它们都允许您通过博客 RSS Feed 发布内容,但这必须符合它们的规范。幸运的是,WordPress 允许开发人员修改默认的 RSS Feed 结构。


第 1 步默认 WordPress RSS 提要结构

默认情况下,WordPress 附带各种提要。在本教程中,如果满足以下条件,我们将使用 http://example.com/?feed=rss2http://example.com/feed/ 上提供的 RSS 2.0 feed您使用永久链接。此提要是一个简单的 XML 文档,结构如下:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
  <!-- these are the namespaces -->
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>

	<channel>
		<!-- this is the head -->
		<title>Your Blog Title</title>
		<atom:link href="http://your-site-url.com/feed" rel="self" type="application/rss+xml" />
		<link>http://your-site-url.com</link>
		<description>your Blog Description</description>
		<lastBuildDate>Thu, 27 Sep 2012 18:30:06 +0000</lastBuildDate>
		<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
		<generator>http://wordpress.org/?v=3.4.2</generator>

		<!-- this is the first post -->
		<item>
			<title>Post 1 Title</title>
			<link>http://your-site-url.com/post-1-slug</link>
			<comments>http://your-site-url.com/post-1-slug#comments</comments>
			<pubDate>Tue, 15 May 2012 13:47:12 +0000</pubDate>
			<dc:creator>John Doe</dc:creator>
			<category><![CDATA[Category 1]]></category>
			<guid isPermaLink="false">http://your-site-url.com/?p=1</guid>
			<description><![CDATA[Aliquam rutrum placerat aliquet. Maecenas congue felis erat]]></description>
			<content:encoded><![CDATA[<p>Aliquam rutrum placerat aliquet. Maecenas congue felis erat.</p>]]></content:encoded>
			<wfw:commentRss>http://your-site-url.com/post-1-slug/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		</item>

		<!-- this is the second post -->
		<item>
			<title>Post 2 Title</title>
			<link>http://your-site-url.com/post-2-slug</link>
			<comments>http://your-site-url.com/post-2-slug#comments</comments>
			<pubDate>Tue, 15 May 2012 13:37:56 +0000</pubDate>
			<dc:creator>John Doe</dc:creator>
			<category><![CDATA[Category 1]]></category>
			<category><![CDATA[Category 2]]></category>
			<guid isPermaLink="false">http://your-site-url.com/?p=2</guid>
			<description><![CDATA[Aliquam rutrum placerat aliquet.]]></description>
			<content:encoded><![CDATA[<p>Aliquam rutrum placerat aliquet</p>]]></content:encoded>
			<wfw:commentRss>http://your-site-url.com/post-2-slug/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		</item>
	</channel>

</rss>
登录后复制

如您所见,每个 元素代表一个 Post 并包含多个子元素,每个子元素都与该 Post“组件”相关。主要有:

  • 是帖子标题
  • 是帖子固定链接
  • 是 RFC822 格式的发布日期
  • 是帖子作者姓名
  • 是元素的子集,每个帖子类别一个
  • 是没有 HTML 标签的帖子摘录
  • 是带有 HTML 标签的整个帖子内容

第 2 步自定义 Flipboard 的 RSS 源

根据 Flipboard 技术要求,可以增强内容。

通过在文章标记中提供额外的语义,添加指定引文、幻灯片和其他设计元素的功能

这些附加语义是:

  • 标题和副标题
  • 引用
  • 图像、视频和音频资源
  • 幻灯片
  • 地理信息

我们可以通过插件在 RSS Feed 中实现这些语义。如前所述,WordPress 提供了特定的 Hook,允许您修改默认的 RSS Feed 结构:

  • rss2_ns - 允许在根 XML 元素内添加新的命名空间;
  • rss2_head - 允许在 feed 标头中添加标签;
  • the_content_feed - 允许修改 Feed 中显示的每个帖子的内容;
  • rss2_item - 允许向每个 (Post) 元素添加新的子元素;

创建一个名为flipboard-feed.php的新文件,打开您最喜欢的文本编辑器并粘贴此插件标头:

<?php
/*
 * Plugin Name:   Flipboard RSS Feed
 * Plugin URI:    http://www.studio404.it
 * Description:   A plugin to customize the default RSS Feed according to Flipboard technical specifications.
 * Version:       1.0
 * Author:        Claudio Simeone
 * Author URI:    http://www.studio404.it
 */
?>
登录后复制

复制 /wp-content/plugins/ 目录中的文件并从插件管理页面激活它。

标题和副标题

如果您想在帖子内容之前添加标题和副标题,则必须添加如下内容:

<hgroup>
	<h1>Title of the Post</h1>
	<h2>This is the Post subtitle</h2>
</hgroup>
登录后复制

您也可以在文本编辑器中手动将其添加到帖子内容中,但这不是最佳解决方案,因为这些标签也会显示在您的网站上(除非您不隐藏 hgroup 元素通过 CSS 样式)。因此,要自动实现此目的并且仅在 RSS 源中,最好使用

元素的帖子标题和

副标题的自定义字段.

在编辑帖子页面中,添加 flipboard_subtitle 自定义字段。

扩展默认的WordPress RSS Feed

将这些行添加到我们的 Flipboard RSS Feed 插件中:

add_filter( 'the_content_feed', 'flipboard_title_and_subtitle' );

function flipboard_title_and_subtitle( $content ) {
	global $post;
	$post_subtitle = get_post_meta( $post->ID, 'flipboard_subtitle', TRUE );
	// add hgroup only if the Custom Field is set
	if ( $post_subtitle ) {
		$hgroup = '<hgroup><h1>' . $post->post_title . '</h1>';
		$hgroup .= '<h2>' . $post_subtitle . '</h2></hgroup>';
		return $hgroup . $content;
	} else {
		return $content;
	}
}
登录后复制

现在,如果您发布帖子并刷新 RSS Feed 页面源,您将在帖子内容之前看到 hgroup 标记。

扩展默认的WordPress RSS Feed

引用

对于拉引号,您可以在帖子内容中使用

标记来指出文本的某​​些部分。我们可以利用该插件将
替换为
扩展默认的WordPress RSS Feed扩展默认的WordPress RSS Feed扩展默认的WordPress RSS Feed扩展默认的WordPress RSS Feed

以上就是扩展默认的WordPress RSS Feed的详细内容,更多请关注php中文网其它相关文章!

WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号