小工具      在线工具  汉语词典  dos游戏  css  js  c++  java

wordpress 自定义_如何在WordPress中创建自定义RSS源

java,python,php,linux,数据库 额外说明

收录于:17天前

WordPress 定制

WordPress comes with built-in default RSS订阅. You can tweak the default feeds by 将自定义内容添加到您的 RSS 源, or even 将帖子缩略图添加到您的 RSS 源. The default RSS and Atom feeds are enough for most users, but you may wish to create a custom RSS feed for delivering specific type of content. In this article, we will show you how to create custom RSS feeds in WordPress.

WordPress带有内置的默认RSS订阅 。 您可以通过将自定义内容添加到您的 RSS 源 ,甚至 RSS订阅 添加帖子缩略图到调整默认Feed。 对于大多数用户而言,默认的RSS和Atom提要就足够了,但是您可能希望创建一个自定义RSS提要来传递特定类型的内容。 在本文中,我们将向您展示如何在WordPress中创建自定义RSS feed。

Please note that this tutorial is not intended for beginner level WordPress users. If you are a beginner, and still want to try it, then please do so on a 本地安装.

请注意,本教程不适用于初学者WordPress用户。 如果您是初学者,但仍想尝试,请在本地安装

As always, you must create a 完整备份您的 WordPress 网站 before making any major changes to a live website.

与往常一样,您必须对于 WordPress 网站创建完整备份,然后再对实时网站进行任何重大更改。

话虽如此,让我们开始在 WordPress 中创建第一个自定义 RSS 源。

话虽如此,让我们开始在 WordPress 中创建第一个自定义 RSS 源。

假设您要创建一个新的 RSS 源,仅显示以下信息:

假设您想要创建一个仅显示以下信息的新 RSS 源:

  • Title

    标题
  • Link

    链接
  • Published Date

    发布日期
  • Author

    作者
  • Excerpt

    摘抄

First thing you need to do is create the new RSS feed in your theme’s functions.php file or in a 特定于站点的插件:

您需要做的第一件事是在主题的functions.php文件或具体的在网站的插件中创建新的RSS feed:



add_action('init', 'customRSS');
function customRSS(){
        add_feed('feedname', 'customRSSFunc');
}


The above code triggers the customRSS function, which adds the feed. The add_feed function has two arguments, feedname, and a callback function. The feedname will make up your new feed url yourdomain.com/feed/feedname and the callback function will be called to actually create the feed. Make a note of the feedname, as you’ll need this later on.

上面的代码触发了customRSS函数,该函数添加了提要。 add_feed函数有两个参数,feedname和一个回调函数。 提要名称将构成您的新提要URL yourdomain.com/feed/feedname并且将调用回调函数来实际创建提要。 记下提要名称,因为稍后将需要它。

Once you have initialized the feed, you’ll need to create the callback function to produce the required feed, using the following code in your theme’s functions.php file or in a site specific plugin:

提要初始化之后,您需要使用主题主题的functions.php文件或特定于站点的插件中的以下代码,创建回调函数以生成所需的提要:


function customRSSFunc(){
        get_template_part('rss', 'feedname');
}

The code above is using the get_template_part function to link to a separate template file, however you can also place the RSS code directly into the function. By using get_template_part, we can keep the functionality separate to the layout. The get_template_part function has two arguments, slug and name, that will look for a template file with the name in the following format, starting with the file at the top (if it doesn’t find the first, it will move on to the second, and so on):

上面的代码使用get_template_part函数链接到单独的模板文件,但是您也可以将RSS代码直接放入函数中。 通过使用get_template_part ,我们可以使功能与布局分开。 get_template_part函数有两个参数slug和name,它们将以以下格式查找名称为以下格式的模板文件,从顶部的文件开始(如果找不到第一个文件,则将移至第二个文件) , 等等):

  1. wp-content/themes/child/rss-feedname.phpwp-content/themes/child/rss-feedname.php
  2. wp-content/themes/parent/rss-feedname.phpwp-content/themes/parent/rss-feedname.php
  3. wp-content/themes/child/rss.phpwp-content/themes/child/rss.php
  4. wp-content/themes/parent/rss.phpwp-content/themes/parent/rss.php

出于本教程的目的,最好将 slug 设置为您要创建的提要类型(在本例中为:rss),并将名称设置为之前配置的提要名称。

出于本教程的目的,最好将 slug 设置为您要创建的提要类型(在本例中为 rss),并将名称设置为您之前配置的提要名称。

Once you’ve told WordPress to look for the feed template, you’ll need to create it. The below code will produce the layout for the feed with the information we listed earlier. Save this file in your theme folder as the slug-name.php template file configured in the get_template_part function.

告诉WordPress查找提要模板后,您需要创建它。 以下代码将使用我们之前列出的信息来生成Feed的布局。 将此文件保存为主题文件夹中的文件,作为在get_template_part函数中配置的slug-name.php模板文件。


<?php
/**
 * Template Name: Custom RSS Template - Feedname
 */
$postCount = 5; // The number of posts to show in the feed
$posts = query_posts('showposts=' . $postCount);
header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>
<rss version="2.0"
        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/"
        <?php do_action('rss2_ns'); ?>>
<channel>
        <title><?php bloginfo_rss('name'); ?> - Feed</title>
        <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
        <link><?php bloginfo_rss('url') ?></link>
        <description><?php bloginfo_rss('description') ?></description>
        <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
        <language><?php echo get_option('rss_language'); ?></language>
        <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
        <sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
        <?php do_action('rss2_head'); ?>
        <?php while(have_posts()) : the_post(); ?>
                <item>
                        <title><?php the_title_rss(); ?></title>
                        <link><?php the_permalink_rss(); ?></link>
                        <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
                        <dc:creator><?php the_author(); ?></dc:creator>
                        <guid isPermaLink="false"><?php the_guid(); ?></guid>
                        <description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
                        <content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
                        <?php rss_enclosure(); ?>
                        <?php do_action('rss2_item'); ?>
                </item>
        <?php endwhile; ?>
</channel>
</rss>

This template code will generate an RSS feed following the above layout. The postCount variable allows you to control the number of posts to display in your feed. The template can be amended as required to display whatever information you require (e.g. post images, comments, etc).

此模板代码将按照上述布局生成RSS源。 postCount变量使您可以控制要在Feed中显示的帖子数。 可以根据需要修改模板,以显示所需的任何信息(例如,帖子图像,评论等)。

The the_excerpt_rss function will display the excerpt of each post, and for posts that do not have excerpts, it will display the first 120 words of the post’s content.

the_excerpt_rss函数将显示每个帖子的摘录,对于没有摘要的帖子,它将显示帖子内容的前120个单词。

Finally, to display your feed, you’ll first need to flush your WordPress rewrite rules. The easiest way to do this is by logging in to the WordPress admin, and clicking 设置 -> 固定链接. Once here, just click 保存更改, which will flush the rewrite rules.

最后,要显示您的供稿,您首先需要刷新WordPress重写规则。 最简单的方法是登录WordPress管理员,然后单击设置->固定链接 。 进入此处后,只需点击保存更改 ,这将刷新重写规则。

You can now access your new feed at yourdomain.com/feed/feedname, where feedname was the feedname you gave in the add_feed function earlier on.

现在,您可以在yourdomain.com/feed/feedname访问新的提要,其中提要名是您先前在add_feed函数中指定的add_feed

The W3C offers a 饲料验证服务, allowing you to validate the resulting feed.

W3C提供了饲料验证服务 ,使您可以验证生成的提要。

故障排除 Troubleshooting
  • 我在尝试查看我的 Feed 时收到 404 错误! 我在尝试查看 Feed 时收到 404 错误!
    • add_feed functionadd_feed函数中提供的
    • If you have the correct feedname, your rewrite rules may not have flushed correctly. Re-save your permalinks just to be sure.

      如果您使用正确的Feed名称,则您的重写规则可能未正确刷新。 请务必重新保存您的永久链接。
    • add_feed function.add_feed函数之后添加代码。
    
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
    
    
  • Once you’ve added this, reload your WordPress site. 注意:使用后应立即除去。一次就足以刷新规则。
  • 添加完毕后,请重新加载WordPress网站。 注意:使用后应立即将其取下。 刷新规则一次就够了。
  • 我的 Feed 未验证! 我的 Feed 无法验证!
    • Using the W3C feed validator, specific details should be given where your feed isn’t validating. Edit the feed template file to resolve these issues

      使用W3C提要验证器,应在不验证提要的地方给出特定的详细信息。 编辑提要模板文件以解决这些问题
  • 我收到 <language /> 验证错误! 我收到 <language /> 验证错误!
    • functions.php file, to update the language option.functions.php文件中,以更新语言选项。
    
    function rssLanguage(){
            update_option('rss_language', 'en');
    }
    add_action('admin_init', 'rssLanguage');
    
    
  • RSS 语言代码.RSS 语言代码的完整列表。
  • Once the above code has been added to your functions file, load the WordPress admin screen for it to take effect. 此后,代码应该从您的 WordPress 函数文件中删除。加载一次就足以配置 rss_language 设置。
  • 将以上代码添加到函数文件后,请加载WordPress管理屏幕以使其生效。 之后,应从 WordPress 函数文件中删除该代码。 加载一次就足以配置 rss_language 设置。
  • This can also be done directly in the database, by looking for the rss_language option in the wp_options table.

    通过在wp_options表中查找rss_language选项,也可以直接在数据库中完成此操作。
  • 我们希望本文能帮助您在 WordPress 中创建自己的自定义 RSS 源。请在下面留下评论,让我们知道您将如何以及为何在 WordPress 网站上使用自定义 RSS 源。

    我们希望本文能帮助您在 WordPress 中创建自己的自定义 RSS 源。请在下面留下评论,让我们知道您如何以及为何在 WordPress 网站上使用自定义 RSS 源。

    翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-rss-feeds-in-wordpress/

    WordPress 定制

. . .

相关推荐

额外说明

RocketMQ主从同步机制源码分析及要点总结(二)

这篇博客接着上篇的讲,主要讲以下两个问题: Slave接收到Master的数据后的处理 同步/异步Master (SYNC_MASTER) 下的主从同步机制及其区别 Slave端为SocketChannel注册了SelectionKey.OP_READ事

额外说明

AQS——ReentrantLock源码中的线程中断补充

文章目录 问题来源 关于线程唤醒 中断唤醒演示 LockSupport.unpark 唤醒阻塞线程 中断唤醒阻塞线程 为什么会有 selfInterrupt() 操作? 使用 lockInterruptibly() 原理图分析 正常情况下的队列 假设T2

额外说明

oracle创建emp表、dept表

 废话不多说 直接上SQL   /*创建empz表*/ CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL, ENAME VARCHAR2(10), JOB VARCHAR2(9), MGR NUMBER(4), HIR

额外说明

[Eigen中文文档] 求解稀疏线性系统

文档总目录 本文目录 稀疏求解器列表 内置直接求解器 内置迭代求解器 外部求解器的包装器 稀疏求解器概念 计算步骤 基准测试例程 英文原文(Solving Sparse Linear Systems) 在Eigen中,有多种方法可用于求解稀疏系数矩阵的线

额外说明

MXNet中使用双向循环神经网络BiRNN对文本进行情感分类<改进版>

在上一节的情感分类当中,有些评论是负面的,但预测的结果是正面的,比如,"this movie was shit"这部电影是狗屎,很明显就是对这部电影极不友好的评价,属于负类评价,给出的却是positive。 所以这节我们通过专门的“分词”和“扩大词向量维

额外说明

Go语言fmt包Printf方法详解

Go语言的标准输出流在打印到屏幕时有些参数跟别的语言(比如C#和Java)不同,下面是我整理的一些常用的格式化输入操作。 General %v 以默认的方式打印变量的值 %T 打印变量的类型 Integer %+d 带符号的整型,fmt.Printf("

额外说明

React native 中 StackNavigator和TabNavigator结合使用

将TabNavigator 作为个一个screen ,然后与stack screen 合成一个stack navigator 。 sample: https://github.com/azhon/ReactNative/blob/master/Stack

额外说明

【Java 基础】for 循环、嵌套循环详解(附案例)

《Java 零基础入门到精通》专栏持续更新中。通过本专栏你将学习到 Java 从入门到进阶再到实战的全套完整内容,所有内容均将集中于此专栏。无论是初学者还是有经验的开发人员,都可从本专栏获益。 订阅专栏后添加我微信或者进交流群,进群可找我领取 前端/Ja

额外说明

Windows系统丢失propsys.dll文件导致功能异常问题

其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个propsys.dll文件(

ads via 小工具