WordPress RSS 피드를 표시하는 방법
안녕하세요 저는 웹사이트와 블로그를 가지고 있는데, 제 웹사이트에 제가 직접 호스팅하는 워드프레스 블로그를 게시하고 싶습니다.
- 저는 제 웹사이트에 게시물을 3개만 표시하고 싶습니다.
- 웹 사이트를 새로고침할 때마다 새로운 게시물이 있는지 자동으로 확인하고 최근 3개만 표시되도록 하고 싶습니다.
- 나는 내 워드프레스 블로그 포스트의 완전한 제목과 구체적인 설명문을 보여주고 싶다.
- 또한 서술은 "..."로 끝나는 비사전 단어가 아닌 단어로 끝나야 합니다.
어떻게 하면 좋을까요, RSS로 할 수 있다고 들었습니다만, 누구 좀 도와 주실 수 있겠습니까?
이를 위해서는 블로그의 RSS를 읽고 RSS에서 제목과 설명을 읽어야 하며, 전체 설명과 제목을 읽은 후 원하는 글자 수로 설명을 잘라야 합니다.그 후 설명의 마지막 단어가 완료되었는지 여부를 확인하고 완료되지 않은 경우 마지막 단어를 삭제하고 "..."를 넣어야 합니다.
먼저 설명을 자르고 마지막에 "..."를 삽입하는 스크립트를 만듭니다.
<?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;
}
이제 값을 저장할 변수를 정의합니다.-
$xml=("http://your-blog-path/rss/");
global $item_title, $item_link, $item_description;
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$x=$xmlDoc->getElementsByTagName('item');
이제 배열을 만들고 그 안에 값을 저장합니다.네가 길을 물어봤기 때문에 나는 3개만 가져갈게요.원하는 대로 변경할 수 있습니다(표시할 게시물 수, 루프에 넣기).
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;
}
?>
이 모든 값을 에코합니다.Link는 사용자가 클릭하는 값이며 사용자는 블로그로 이동합니다.-
첫 번째 최신 투고:
<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); ?>
이것으로 당신의 문제가 해결되기를 바랍니다.그나저나 좋은 질문이야.
PHP를 사용한 RSS 피드 표시에 대한 원본 문서를 보려면 여기를 클릭하십시오.
장고 어나니머스substrwords설명을 트리밍하고 삽입하기 위해 사용되는...설명의 마지막에, 그것이 다음을 통과했을 경우$maxchar가치.
풀코드:
블로그.개요
<?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://wordpress.org/news/feed/'); // <-- 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 = 3; // <-- 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, 100);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
별도의 PHP 파일에 쉽게 넣을 수 있습니다.blog.php실제 페이지 내에서 호출합니다.
예:
social.displaces
<h3>Latest blog post:</h3>
<?php require 'blog.php' ?>
또한 이 코드는 플러그 앤 플레이에 적합합니다.
Wordpress REST API를 사용하여 게시물을 검색해 보십시오.
API URL은 https://public-api.wordpress.com/rest/v1/sites/$site/posts/ 입니다.
$site는 워드프레스 블로그의 사이트 ID입니다.
또는 단순히 이 플러그인을 사용합니다.
http://www.codehandling.com/2013/07/wordpress-feeds-on-your-website-with.html
언급URL : https://stackoverflow.com/questions/9224280/how-to-display-wordpress-rss-feed-your-website
'programing' 카테고리의 다른 글
| angular.grid를 작성하기 위한 ng-timeout (0) | 2023.04.02 |
|---|---|
| 각도란글로벌 키보드 단축키를 만드는 JS 방법 (0) | 2023.04.02 |
| 리액트 라우터 V4를 사용하여 프로그램 탐색 (0) | 2023.04.02 |
| 오라클 시퀀스의 현재 값을 증가시키지 않고 취득하려면 어떻게 해야 합니까? (0) | 2023.04.02 |
| JSON.string 딥 오브젝트 (0) | 2023.04.02 |