如何在您的网站上显示 WordPress RSS 提要? [英] How to display WordPress RSS feed your website?

查看:52
本文介绍了如何在您的网站上显示 WordPress RSS 提要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个网站和一个博客,我想在我的网站上显示我自己托管的 wordpress 博客.

Hello i have a website and a blog, i want to display my self hosted wordpress blog on my website.

  1. 我只想在我的网站上显示 3 个帖子.
  2. 我想在每次重新加载我的网站时自动检查任何新帖子,以便只显示最近的三个帖子.
  3. 我想显示我的 wordpress 博客文章的完整标题,但要显示特定的描述字母.
  4. 此外,描述应该以一个词结尾,而不是一些以..."结尾的非字典词

如何做到这一点,我听说可以通过 RSS 完成.有人可以帮我吗?

How this can be done, i have heard that it can be done through RSS. Can somebody help me?

推荐答案

要做到这一点,你需要阅读博客的RSS,从RSS你需要阅读标题和描述,阅读整个描述和标题后你需要将描述修剪为您想要的字母数.之后您需要检查天气描述最后一个单词是否已完成,然后您需要删除最后一个单词如果未完成并输入...".

To accomplish this you need to read the RSS of the blog, from RSS you need to read the Title and the description, after reading the whole description and title you need to trim the description to your desired number of letters. After that you need to check weather the description last word has been completed or not and then you need to remove a the last word if not completed and put the "...".

首先我们将编写一个脚本来修剪描述并将..."放在最后:-

First we will make a script to trim the description and to put "..." in last:-

<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
    if (strlen($text) > $maxchar || $text == '') {
        $words = preg_split('/\s/', $text);      
        $output = '';
        $i      = 0;
        while (1) {
            $length = strlen($output)+strlen($words[$i]);
            if ($length > $maxchar) {
                break;
            } 
            else {
                $output .= " " . $words[$i];
                ++$i;
            }
        }
        $output .= $end;
    } 
    else {
        $output = $text;
    }
    return $output;
}

现在我们将定义存储值的变量:-

Now we will define the variables in which we store the values:-

$xml=("http://your-blog-path/rss/");
global $item_title, $item_link, $item_description;

$xmlDoc = new DOMDocument();

$xmlDoc->load($xml);

$x=$xmlDoc->getElementsByTagName('item');

现在,我们将创建一个数组并在其中存储值.我只拿 3 个,因为你问了它的方式.您可以将其更改为任何内容(您要显示的帖子数量,将其放入循环中)

Now, we will make an array and store values in it. I am only taking 3 because you have asked it the way. You can change it to anything (The number of post you want to show, put that in the loop)

for ($i=0; $i<3; $i++)

{

$item_title[$i] = $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;

$item_link[$i] = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;

$item_description[$i] = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;

}

?>

现在回显所有这些值,链接是您的用户将点击并将被带到您的博客的值:-

Now echo all these values, Link is the value where your user will click and he will be taken to your blog:-

最近的第一篇文章:

<a href="<?php echo $item_link[0]; ?>"><?php echo $item_title[0]; ?></a>
<?php echo substrwords($item_description[0],70); ?>

最近的第二篇文章:

<a href="<?php echo $item_link[1]; ?>"><?php echo $item_title[1]; ?></a>
<?php echo substrwords($item_description[1],70); ?>

最近的第三篇帖子:

<a href="<?php echo $item_link[2]; ?>"><?php echo $item_title[2]; ?></a>
<?php echo substrwords($item_description[2],70); ?>

希望这可以解决您的问题.顺便问一下好问题.

Hope this can solve your problem. By the way Nice question.

这篇关于如何在您的网站上显示 WordPress RSS 提要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆