友链的朋友圈 RSS的聚合页 (WordPress->PIX主题篇)

本文代码基于WordPress构建,PIX主题测试,其他主题理论上通用。

代码完全基于PHP或者WordPress默认的函数或者变量编写,是页面代码,需要新建一个页面,模板选择这个friends模板。

一、运行逻辑:

代码一自动获取后台所有友链,如果友链RSS地址已设置,解析地址获取内容,如果友链RSS地址未设置,查找所有可能的RSS路径进行探测性获取,所有内容获取完成后,按照更新时间(考虑到有些网站同一篇文章内容可能经常更新,由发布时间变更为更新时间)降序排列,取总量前50篇进行展示。

代码二只针对RSS地址已设置的友链,未设置的全部略过,大大减轻了服务器的负担,因此强烈建议添加友链时,直接在后天填写友链网站的RSS地址

二、已知问题:

代码一问题1,由于采用了缓存机制,第一次运行非常缓慢,和友链数目及RSS内容数量有关,预计20秒-120秒左右,并概率性出现502错误,重新刷新即可,第一次缓存完后,访问速度飞快。缓存设定为2小时(代码中可更改,由详细注释),2小时候如果重新刷新或访问页面,重新获取最新内容。问题2,部分主题下有些友链有RSS但是获取不到,我刚开始以为是代码问题,但是经过测试,同样的代码,有的主题能够完全获取友链内容,有些主题只能获取部分友链内容。

代码二大概率避免了502错误,但是有些友链的RSS还是获取不到。

三、代码如下(推荐代码二和代码三,两者区别是代码二是单线程,代码三采用了异步加载的模式同时处理几个友链网站,执行更加高效,这个采用哪个看服务器配置了,一般代码二足矣。)

在自己的主题页面目录(一般为page或pages)下新建friends.php,写入以下代码:

代码一:

<?php
/*
Template Name: 大海博客-RSS朋友圈
*/
date_default_timezone_set('Asia/Shanghai'); // 设置时区为上海,即中国标准时间
get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

        <?php
        // 检查是否有缓存数据可用
        $cache_key = 'friend_link_rss_cache';
        $cache_data = wp_cache_get($cache_key);

        if (!$cache_data) {
            // 获取所有友情链接
            $args = array(
                'orderby' => 'name',
                'category' => 0,
                'order' => 'ASC',
                'hide_invisible' => 1,
                'categorize' => 0,
                'title_li' => '',
                'echo' => 0
            );
            $links = get_bookmarks($args);

            // 存储所有文章数据的数组
            $all_articles = array();

            // 遍历每个友情链接
            foreach ($links as $link) {
                $domain = parse_url($link->link_url, PHP_URL_HOST); // 获取链接的域名网址

                // 解析链接失败时跳过该链接
                if (!$domain) {
                    continue;
                }

                // 获取所有可能的feed地址
                $feed_urls = array(
                    "https://{$domain}/feed/",
                    "https://{$domain}/rss/",
                    "https://{$domain}/rss.xml",
                    "https://{$domain}/atom.xml",
                    "https://{$domain}/feed/rss/",
                    "https://{$domain}/feed/atom/",
                    "https://{$domain}/feed/rss2/",
                    "https://{$domain}/feed/atom2/"
                );

                // 遍历每个feed地址,获取RSS内容
                foreach ($feed_urls as $feed_url) {
                    $rss = fetch_feed($feed_url);

                    // 检查是否发生错误
                    if (!is_wp_error($rss)) {
                        $rss->set_cache_duration(3600); // 设置缓存时间为1小时
                        $rss->force_feed(true); // 强制使用RSS 2.0格式
                        $rss->enable_order_by_date(true); // 启用按日期排序

                        $rss_items = $rss->get_items(); // 获取所有文章

                        foreach ($rss_items as $item) {
                            $item_title = $item->get_title(); // 文章标题
                            $item_link = $item->get_permalink(); // 文章链接
                            $item_date = $item->get_date('Y-m-d H:i:s'); // 更新时间
                            $item_author = $item->get_author()->get_name(); // 作者
                            $item_excerpt = $item->get_description(); // 文章摘要
                            $item_content = $item->get_content(); // 文章内容

                            // 将文章数据存入数组
                            $all_articles[] = array(
                                'link_name' => $link->link_name,
                                'link_logo' => $link->link_image,
                                'item_title' => $item_title,
                                'item_link' => $item_link,
                                'item_date' => $item_date,
                                'item_author' => $item_author,
                                'item_excerpt' => $item_excerpt,
                                'item_content' => $item_content
                            );
                        }

                        break; // 如果成功获取到内容,则跳出循环
                    }
                }
            }

            // 根据更新时间降序排列所有文章数据
            usort($all_articles, function ($a, $b) {
                return strtotime($b['item_date']) - strtotime($a['item_date']);
            });

            // 存储数据到缓存
            wp_cache_set($cache_key, $all_articles, '', 2 * HOUR_IN_SECONDS);
        } else {
            $all_articles = $cache_data;
        }

        // 输出前50篇文章信息
        $articles_to_show = array_slice($all_articles, 0, 50);
        foreach ($articles_to_show as $key => $article) {

            echo '<div class="friend-link-rss" style="border: 1px dashed #ccc; background-color: #f5f5f5; padding: 10px; margin-bottom: 20px; display: flex;">';

            // 左边占1/5
            echo '<div style="width: 20%; padding-right: 10px;">';
            // 上部分显示站标logo,大小控制在60*60
            echo '<div style="display: flex; align-items: center; justify-content: center; height: 50%;">';
            echo '<img src="' . $article['link_logo'] . '" alt="' . $article['link_name'] . '" style="width: 60px; height: 60px;">';
            echo '</div>';
            // 下部分显示网站名称,显示在左边下半部的正中间,加粗且为红色
            echo '<p style="font-size: 12px; text-align: center; margin: 0; height: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold; color: red;">' . $article['link_name'] . '</p>';
            echo '</div>';

            // 右边占4/5
            echo '<div style="width: 80%;">';
            // 上部分显示文章名称,点击文章名称打开新窗口跳转到文章实际地址,文字蓝色大小为16px
            echo '<h3 style="font-size: 16px; color: blue; margin-bottom: 5px;"><a href="' . $article['item_link'] . '" target="_blank">' . $article['item_title'] . '</a></h3>';
            // 中部分显示文章摘要显示65个文字大小为14px,如果没有摘要,取文章的前65个文字
            echo '<p style="font-size: 14px; margin-bottom: 5px;">' . wp_trim_words(wp_kses_post(empty($article['item_excerpt']) ? $article['item_content'] : $article['item_excerpt']), 65) . '</p>';
            // 下部分显示作者名称和更新时间,同一行
            echo '<p style="font-size: 14px; margin-bottom: 0;">作者:' . $article['item_author'] . ' | 更新时间:' . $article['item_date'] . '</p>';
            echo '</div>';

            echo '</div>';
        }
        ?>
        <?php get_sidebar(); ?>
    </main><!-- #main -->
</div><!-- #primary -->

<?php get_footer(); ?>

代码二:

<?php
/*
Template Name: 大海博客-RSS朋友圈
*/
date_default_timezone_set('Asia/Shanghai');
get_header(); 
require_once(ABSPATH . WPINC . '/class-simplepie.php');
?>


<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

        <p style="color: red; font-size: 14px; margin-top: 10px; margin-bottom: 20px; text-align: center; display: flex; align-items: center; justify-content: center;">以下友情链接网站最新内容每2小时获取更新一次</p>

        <?php
        // 检查是否有缓存数据可用
        $cache_key = 'friend_link_rss_cache';
        $cache_data = wp_cache_get($cache_key);

        if (!$cache_data) {
            // 获取所有友情链接的操作放在定时任务中进行
            // 将结果存储在数据库中,页面只需要从数据库中读取数据即可
            $all_articles = get_friend_link_rss_data();

            // 存储数据到缓存
            wp_cache_set($cache_key, $all_articles, '', 2 * HOUR_IN_SECONDS);
        } else {
            $all_articles = $cache_data;
        }

        // 输出前50篇文章信息
        $articles_to_show = array_slice($all_articles, 0, 50);
        foreach ($articles_to_show as $key => $article) {
            ?>
            <div class="friend-link-rss" style="border: 1px dashed #ccc; background-color: #f5f5f5; padding: 10px; margin-bottom: 20px; display: flex;">

                <!-- 左边占1/5 -->
                <div style="width: 20%; padding-right: 10px;">
                    <!-- 上部分显示站标logo,大小控制在60*60 -->
                    <div style="display: flex; align-items: center; justify-content: center; height: 50%;">
                        <a href="<?php echo $article['item_link']; ?>" target="_blank"><img src="<?php echo $article['link_logo']; ?>" alt="<?php echo $article['link_name']; ?>" style="width: 60px; height: 60px;"></a>
                    </div>
                    <!-- 下部分显示网站名称,显示在左边下半部的正中间,加粗且为红色 -->
                    <p style="font-size: 12px; text-align: center; margin: 0; height: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold; color: red;"><a href="<?php echo $article['link_url']; ?>" target="_blank"><?php echo $article['link_name']; ?></a></p>
                </div>

                <!-- 右边占4/5 -->
                <div style="width: 80%;">
                    <!-- 上部分显示文章名称,点击文章名称打开新窗口跳转到文章实际地址,文字蓝色大小为16px -->
                    <h3 style="font-size: 16px; color: blue; margin-bottom: 5px;"><a href="<?php echo $article['item_link']; ?>" target="_blank"><?php echo $article['item_title']; ?></a></h3>
                    <!-- 中部分显示文章摘要显示65个文字大小为14px,如果没有摘要,取文章的前65个文字 -->
                    <p style="font-size: 14px; margin-bottom: 5px;"><?php echo wp_trim_words(wp_kses_post(empty($article['item_excerpt']) ? $article['item_content'] : $article['item_excerpt']), 65); ?></p>
                    <!-- 下部分显示作者名称和更新时间,同一行 -->
                    <p style="font-size: 14px; margin-bottom: 0;">作者:<?php echo $article['item_author']; ?> | 更新时间:<?php echo $article['item_date']; ?></p>
                </div>

            </div>
        <?php
        }
        ?>
        <?php get_sidebar(); ?>
    </main><!-- #main -->
</div><!-- #primary -->

<?php get_footer(); ?>

<?php
function get_friend_link_rss_data() {
    // 获取所有友情链接
    $args = array(
        'orderby' => 'name',
        'category' => 0,
        'order' => 'ASC',
        'hide_invisible' => 1,
        'categorize' => 0,
        'title_li' => '',
        'echo' => 0
    );
    $links = get_bookmarks($args);

    // 存储所有文章数据的数组
    $all_articles = array();

    // 遍历每个友情链接
    foreach ($links as $link) {
        $domain = parse_url($link->link_url, PHP_URL_HOST);

        if (!$domain) {
            continue;
        }

        // 获取feed地址
        $feed_url = $link->link_rss ? $link->link_rss : "https://{$domain}/feed/";

        // 尝试使用https协议获取RSS内容
        $rss_content = get_rss_content($feed_url);

        // 如果https协议获取失败,则尝试使用http协议获取
        if (!$rss_content) {
            $feed_url = str_replace('https://', 'http://', $feed_url);
            $rss_content = get_rss_content($feed_url);
        }

        if ($rss_content) {
            $rss_items = get_rss_items($rss_content);

            foreach ($rss_items as $item) {
                $item_data = array(
                    'link_name' => $link->link_name,
                    'link_logo' => $link->link_image,
                    'link_url' => $link->link_url,
                    'item_title' => $item->get_title(),
                    'item_link' => $item->get_permalink(),
                    'item_date' => $item->get_date('Y-m-d H:i:s'),
                    'item_author' => $item->get_author()->get_name(),
                    'item_excerpt' => $item->get_description(),
                    'item_content' => $item->get_content()
                );

                $all_articles[] = $item_data;
            }
        }
    }

    // 根据更新时间降序排列所有文章数据
    usort($all_articles, function ($a, $b) {
        return strtotime($b['item_date']) - strtotime($a['item_date']);
    });

    return $all_articles;
}

function get_rss_content($feed_url) {
    // 使用更高效的方式获取RSS内容
    $response = wp_remote_get($feed_url, array('sslverify' => false));

    if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
        return wp_remote_retrieve_body($response);
    }

    return false;
}

function get_rss_items($rss_content) {
    // 使用更高效的方式获取RSS项
    $rss = new SimplePie();
    $rss->set_raw_data($rss_content);
    $rss->init();
    $rss->handle_content_type();

    return $rss->get_items();
}

代码三:

<?php
/*
Template Name: 大海博客-RSS朋友圈
*/
date_default_timezone_set('Asia/Shanghai');
get_header(); 
require_once(ABSPATH . WPINC . '/class-simplepie.php');
?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

        <p style="color: red; font-size: 14px; margin-top: 10px; margin-bottom: 20px; text-align: center; display: flex; align-items: center; justify-content: center;">以下友情链接网站最新内容每2小时获取更新一次</p>

        <?php
        // 检查是否有缓存数据可用
        $cache_key = 'friend_link_rss_cache';
        $cache_data = wp_cache_get($cache_key);

        if (!$cache_data) {
            // 获取所有友情链接的操作放在定时任务中进行
            // 将结果存储在数据库中,页面只需要从数据库中读取数据即可
            $all_articles = get_friend_link_rss_data();

            // 存储数据到缓存
            wp_cache_set($cache_key, $all_articles, '', 2 * HOUR_IN_SECONDS);
        } else {
            $all_articles = $cache_data;
        }

        // 输出前50篇文章信息
        $articles_to_show = array_slice($all_articles, 0, 50);
        foreach ($articles_to_show as $key => $article) {
            display_friend_link_rss($article);
        }
        ?>
        <?php get_sidebar(); ?>
    </main><!-- #main -->
</div><!-- #primary -->

<?php get_footer(); ?>

<?php
function get_friend_link_rss_data() {
    // 获取所有友情链接
    $args = array(
        'orderby' => 'name',
        'category' => 0,
        'order' => 'ASC',
        'hide_invisible' => 1,
        'categorize' => 0,
        'title_li' => '',
        'echo' => 0
    );
    $links = get_bookmarks($args);

    // 存储所有文章数据的数组
    $all_articles = array();

    // 遍历每个友情链接
    foreach ($links as $link) {
        $domain = parse_url($link->link_url, PHP_URL_HOST);

        if (!$domain) {
            continue;
        }

        // 获取feed地址
        $feed_url = $link->link_rss ? $link->link_rss : "https://{$domain}/feed/";

        // 尝试使用https协议获取RSS内容
        $rss_content = get_rss_content($feed_url);

        // 如果https协议获取失败,则尝试使用http协议获取
        if (!$rss_content) {
            $feed_url = str_replace('https://', 'http://', $feed_url);
            $rss_content = get_rss_content($feed_url);
        }

        if ($rss_content) {
            $rss_items = get_rss_items($rss_content);

            foreach ($rss_items as $item) {
                $item_data = array(
                    'link_name' => $link->link_name,
                    'link_logo' => $link->link_image,
                    'link_url' => $link->link_url,
                    'item_title' => $item->get_title(),
                    'item_link' => $item->get_permalink(),
                    'item_date' => $item->get_date('Y-m-d H:i:s'),
                    'item_author' => $item->get_author()->get_name(),
                    'item_excerpt' => $item->get_description(),
                    'item_content' => $item->get_content()
                );

                $all_articles[] = $item_data;
            }
        }
    }

    // 根据更新时间降序排列所有文章数据
    usort($all_articles, function ($a, $b) {
        return strtotime($b['item_date']) - strtotime($a['item_date']);
    });

    return $all_articles;
}

function get_rss_content($feed_url) {
    // 使用更高效的方式获取RSS内容
    $response = wp_remote_get($feed_url, array('sslverify' => false));

    if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
        return wp_remote_retrieve_body($response);
    }

    return false;
}

function get_rss_items($rss_content) {
    // 使用更高效的方式获取RSS项
    $rss = new SimplePie();
    $rss->set_raw_data($rss_content);
    $rss->init();
    $rss->handle_content_type();

    return $rss->get_items();
}

function display_friend_link_rss($article) {
    ?>
    <div class="friend-link-rss" style="border: 1px dashed #ccc; background-color: #f5f5f5; padding: 10px; margin-bottom: 20px; display: flex;">

        <!-- 左边占1/5 -->
        <div style="width: 20%; padding-right: 10px;">
            <!-- 上部分显示站标logo,大小控制在60*60 -->
            <div style="display: flex; align-items: center; justify-content: center; height: 50%;">
                <a href="<?php echo $article['item_link']; ?>" target="_blank"><img src="<?php echo $article['link_logo']; ?>" alt="<?php echo $article['link_name']; ?>" style="width: 60px; height: 60px;"></a>
            </div>
            <!-- 下部分显示网站名称,显示在左边下半部的正中间,加粗且为红色 -->
            <p style="font-size: 12px; text-align: center; margin: 0; height: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold; color: red;"><a href="<?php echo $article['link_url']; ?>" target="_blank"><?php echo $article['link_name']; ?></a></p>
        </div>

        <!-- 右边占4/5 -->
        <div style="width: 80%;">
            <!-- 上部分显示文章名称,点击文章名称打开新窗口跳转到文章实际地址,文字蓝色大小为16px -->
            <h3 style="font-size: 16px; color: blue; margin-bottom: 5px;"><a href="<?php echo $article['item_link']; ?>" target="_blank"><?php echo $article['item_title']; ?></a></h3>
            <!-- 中部分显示文章摘要显示65个文字大小为14px,如果没有摘要,取文章的前65个文字 -->
            <p style="font-size: 14px; margin-bottom: 5px;"><?php echo wp_trim_words(wp_kses_post(empty($article['item_excerpt']) ? $article['item_content'] : $article['item_excerpt']), 65); ?></p>
            <!-- 下部分显示作者名称和更新时间,同一行 -->
            <p style="font-size: 14px; margin-bottom: 0;">作者:<?php echo $article['item_author']; ?> | 更新时间:<?php echo $article['item_date']; ?></p>
        </div>

    </div>
    <?php
}
?>

四、运行演示

友链的朋友圈 RSS的聚合页 (WordPress->PIX主题篇)-似水流年
© 版权声明
THE END
喜欢就支持一下吧
点赞15赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容