将Wordpress博客条目添加到自己的网站
我有一些代码,可以将最近十篇Wordpress博客文章发送到我自己的非Wordpress站点。有没有办法显示分配给博客帖子的特色图片?我成功地回应了博客的标题、日期和正文将Wordpress博客条目添加到自己的网站,wordpress,rss,blogs,Wordpress,Rss,Blogs,我有一些代码,可以将最近十篇Wordpress博客文章发送到我自己的非Wordpress站点。有没有办法显示分配给博客帖子的特色图片?我成功地回应了博客的标题、日期和正文 <?php global $text, $maxchar, $end; function substrwords($text, $maxchar, $end='...') { if (strlen($text) > $maxchar || $text
<?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;
}
$rss = new DOMDocument();
$rss->load('http://myblog.wordpress.com/rss/'); // <-- Change feed to your site
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 10; // <-- Change the number of posts shown
for ($x=0; $x<$limit; $x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substrwords($description, 400);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<div style="margin-bottom:25px;">';
echo '<h3><strong><a style="color: #139035;" href="'.$link.'" title="'.$title.'" target="_blank">'.$title.'</a></strong></h3>';
echo '<p><small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
echo '<span> <strong><a target="_blank" style="color: #139035;" href="'.$link.'" title="'.$title.'">Read blog ></span></strong>';
echo '</div>';
}
?>
您可以使用以下代码将特色图像添加到RSS中
function img_in_rss($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ){
$content = get_the_post_thumbnail( $post->ID, 'medium') . $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'img_in_rss');
add_filter('the_content_feed', 'img_in_rss');
WordPress默认RSS源不公开特色图像。你需要在博客上扩展RSS提要的功能。底部的“添加过滤器”位给了我一个错误。你得到了什么错误?我测试它是在向提要中添加特色图像。我使用的是CakePHP,得到的错误是“致命错误:调用未定义的函数add_filter()”。。很抱歉,您需要在WordPress主题的functions.php文件中添加上述代码,RSS是由WP站点生成的,对吗?所以我们只能在那里添加图像。我在哪里可以找到那个文件?我不熟悉Wordpress,我必须建立一个WP博客,为客户提供他们的网站