对于基础知识,我遵循了我在Google上找到的以下指南:
由于您必须自己收集和过滤多个博客中的帖子,因此多站点提要很复杂。为了简化一些命名,假设我想收集所有标记的帖子
foo
.
我写了几个类来管理这个:
FooTagFeed的代码太长,不雅观,无法包含在这里,但我将删除代码,以指导任何面临类似问题的人。
将此添加到我的主题的底部functions.php
文件:
# content/themes/main-theme/functions.php
/***************************************************
* Multi-Site Feed
* Based on http://www.wpbeginner.com/wp-tutorials/how-to-create-custom-rss-feeds-in-wordpress/
***************************************************/
add_action(\'init\', \'add_multisite_foo_feed\');
function add_multisite_foo_feed() {
// Args: path (/feed/foo), callback (function below)
add_feed(\'foo\', \'build_foo_tag_feed\');
}
function build_foo_tag_feed() {
header(\'Content-Type: \' . feed_content_type(\'rss2\') . \'; charset=\' . get_option(\'blog_charset\'), true);
get_template_part(\'foo_tag_feed\');
}
然后是我的feed类:
# content/themes/main-theme/foo_tag_feed.php
Class FooFeedPost {
function __construct($wp_post) {}
function as_rss_item() {}
}
class FooTagFeed {
function __construct($wpdb) {
$this->db = $wpdb;
}
function as_rss2() {
$feed_posts = $this->fetch_posts_tagged_foo();
// Sort by post date in DESC order.
// Source: https://stackoverflow.com/a/10159521/6763239
// Sorts in place and returns boolean.
usort($feed_posts, function($a, $b) {
return $a->timestamp < $b->timestamp;
});
$feed_posts = array_slice($feed_posts, 0, $this->item_limit);
return $this->format_as_rss2($feed_posts);
}
function fetch_posts_tagged_foo() {
$feed_posts = array();
$blog_ids = $this->fetch_blog_ids();
foreach ( $blog_ids as $blog_id ) {
switch_to_blog($blog_id);
$site_posts = $this->fetch_posts_tagged_foo_by_blog($blog_id);
foreach ( $site_posts as $wp_post ) {
$feed_posts[] = new FooFeedPost($wp_post);
}
restore_current_blog();
}
return $feed_posts;
}
function format_as_rss2($feed_posts) {}
function fetch_blog_ids() {}
function fetch_posts_tagged_foo_by_blog($blog_id) {}
}
$foo_feed = new FooFeedPost($wpdb);
echo $foo_feed->as_rss2();
饲料将在以下地点供应:
https://my-multisite-blog.com/feed/foo如果您想看到更多的代码,请点击我,我将尝试包含指向要点或其他内容的链接。