This is my RSS feed format:
<item>
<title></title>
<link></link>
<description></description>
<pubDate></pubDate>
<guid></guid>
<dc:date></dc:date>
</item>
I want to dipsplay the last 7 posts with CSS style, so I use this code:
<?php
$url = "**THE URL I AM SCRAPING DATA FROM**";
$rss = simplexml_load_file($url);
$i = 0;
if (!empty($rss))
{
$site = $rss
->channel->title;
$sitelink = $rss
->channel->link;
foreach ($rss
->channel->item as $item)
{
$title = $item->title;
$link = $item->link;
$description = $item->description;
$item->description = strip_tags($item->description);
$date = $item->pubDate;
$pubDate = date('d.m.Y', strtotime($date));
if ($i >= 7) break;
?>
<div class="post-item">
<div class="post-item-wrap">
<div class="post-image">
<a href="<?php echo $link;?>">
<img alt="" src="images/news/nra.jpg">
</a>
</div>
<div class="post-item-description">
<span class="post-meta-date"><?php echo $pubDate;?></span>
<h2><a href="<?php echo $link ?>" target="_blank"><?php echo $title;?>
</a></h2>
<p><?php echo implode(' ', array_slice(explode(' ', $description), 0, 30)) . "..";?></p>
learn more <i class="icon-chevron-right"></i>
</div>
</div>
</div>
<?php
$i++;
}
}
?>
Now I want each of these 7 posts to have unique id.
I need script to generate item -> title and item -> description depending on the item -> link.
(If I click first xml post for example it will take me to page where I can display the title and description according to which post I clicked)
Thanks in advance.
use this code get last 7 items
array_slice($rss->channel->item, -7);
If url is "rss.php?feed=1" or "rss.php?feed=2"
$rssSno=$_GET['feed'];
$output = array_slice($rss->channel->item, ($rssSno-1),1);
Related
I have set up a blog on a subdomain. like blog.mywebsite.com
And I have set up a recent posts widget using RSS Feed on my main website's homepage.
I have managed to display the most recent 3 posts in that widget.
But now when I add a new post on the subdomain blog site that widget is not getting updated. It is showing only old posts.
I used this tutorial to set up the widget. https://www.worldoweb.co.uk/2012/display-wordpress-posts-on-another-wp-blog
The actual PHP Code I am using on my website is below.
<?php
//replace with your URL
$rss = fetch_feed('https://blog.mywebsite.com/feed/');
if (!is_wp_error($rss)) :
$maxitems = $rss -> get_item_quantity(3); //gets latest 3 items This can be changed to suit your requirements
$rss_items = $rss -> get_items(0, $maxitems);
endif;
?>
<?php
//shortens description
function shorten($string, $length) {
$suffix = '…';
$short_desc = trim(str_replace(array("\r", "\n", "\t"), ' ', strip_tags($string)));
$desc = trim(substr($short_desc, 0, $length));
$lastchar = substr($desc, -1, 1);
if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?')
$suffix = '';
$desc .= $suffix;
return $desc;
}
?>
<!--start of displaying our feeds-->
<ul class="rss-items" id="wow-feed">
<?php
if ($maxitems == 0) echo '<li>No items.</li>';
else foreach ( $rss_items as $item ) :
?>
<li class="item">
<span class="data">
<h3><a href='<?php echo esc_url($item -> get_permalink()); ?>' title='<?php echo esc_html($item -> get_title()); ?>'> <?php echo esc_html($item -> get_title()); ?></a></h3>
<span class="date-image"> </span>
<div class="rtin-date"><?php echo $item -> get_date('F j, Y'); ?></div>
<p class="rtin-content"><?php echo shorten($item -> get_description(), '120'); ?></p>
</span>
</li>
<?php endforeach; ?>
</ul>
What I want to do is.
when i add new post to my blog it should show in that RSS widget too.
A web development dummy here :)
How do I put a php variable inside an html tag? for example, here I want to print each product's name, price, and image
(also could you please suggest whether the way I retrieve the image is correct?)
<?php
$doc = new DOMDocument();
$doc->load('database/products.xml');
$products = $doc->getElementsByTagName("fruit");
foreach ($products as $fruit) {
$names = $fruit->getElementsByTagName("name");
$name = $names->item(0)->nodeValue;
$prices = $fruit->getElementsByTagName("price");
$price = $prices->item(0)->nodeValue;
$images = $fruit->getElementsByTagName("image");
$image = $images->item(0)->nodeValue;
echo "<b>$name - $price - $image\n</b><br>";
echo'
<div class="container">
<a href="p3Apples.html">
<img src="img/'.$image.'" class="item-image">
<div class=‘iamge-title’>$name</div>
<div class=‘item-price’> $.$price </div>
<a href=‘shoppingcart.html’ class=‘b-menu’>
<img id=‘test’ src=‘img/addToCart.png’> </a>
</form>
</a>
</div>
';
};
?>
When using ' variables aren't processed, use " in this case
$doc = new DOMDocument();
$doc->load('database/products.xml');
$products = $doc->getElementsByTagName("fruit");
foreach ($products as $fruit) {
$names = $fruit->getElementsByTagName("name");
$name = $names->item(0)->nodeValue;
$prices = $fruit->getElementsByTagName("price");
$price = $prices->item(0)->nodeValue;
$images = $fruit->getElementsByTagName("image");
$image = $images->item(0)->nodeValue;
echo "<b>$name - $price - $image\n</b><br>";
echo "
<div class='container'>
<a href='p3Apples.html'>
<img src='img/".$image."' class='item-image'>
<div class='iamge-title'>$name</div>
<div class='item-price'> $.".$price."</div>
<a href='shoppingcart.html' class='b-menu'>
<img id='test' src='img/addToCart.png'>
</a>
</form>
</a>
</div>
";
}
I have a problem in foreach in php. i have dynamic data coming from database in the front end. but some paragraph is manual but it should change with the foreach loop.
$para = array
(
array("one","Get an authentic taste of the Malabar Coast as you cruise through the green Nilgiris and feast on the ancient history of the South.","Take a tour through the idyllic beaches of the Konkan Coast, while absorbing the rich culture of the majestic forts and stunning Sahyadri Hills.")
);
$i = 1;
foreach($allRides as $rides){
if ($i == 3) {
$i = 1;
}
$detail = $rides->_field_data['nid']['entity'];
$title = $detail->title;
$noofdays = $detail->field_tour_days['und'][0]['value'];
$noofdetails = $detail->field_more_details_of_the_tour['und'][0]['value'];
$noofdetailshort = $para[0][$i];
$image = image_style_url("homepage_ride",$detail->field_large_image['und'][0]['uri']);
$articleURL = url("node/".$rides->nid,array("absolute"=>true));
?>
<div class="places-slider-list">
<a href="<?php echo $articleURL;?>">
<img src="<?php echo $image;?>" class="img-responsive" alt="<?php echo $title;?>">
</a>
<article class="places-slider-data">
<h4><?php echo $title;?></h4>
<p><?php echo $noofdays;?> Day Guided Tour</p>
View Tour
</article>
<div class="main-slider-sidebar">
<p><?php echo $noofdetailshort;?></p>
</div>
</div>
<?php $i++;} ?>
i want to put this array data with for each loop means its a slider with different price heading and, i want to add statically those paras, but i don't know how to do that.
I'am currently parsing in a twitter tweets by geocode and displaying them using php. But at the moment I'm unable to display the link to the tweet.
But within the XML it is feed->entry->
<link type="text/html" href="http://twitter.com/OFFICIALGRINZ/statuses/165838159928762368" rel="alternate" >
Is is possible to access the url from inside this tag?
Here is my code.
<?php
$tabTitle = ' | Twitter';
$pageIntroductionHeading = 'Twitter';
$pageIntroductionContent = 'Twitter';
$column1Heading = 'London';
$column2Heading = 'New York';
$column3Heading = 'Paris';
require_once 'header.php';
require_once 'navigationMenu.php';
?>
<?php require_once 'getWeather.php'; ?>
<?php require_once 'getExchangeRates.php'; ?>
<?php require_once 'pageIntroduction.php'; ?>
<div id="obsah" class="content box">
<div class="in">
<div class="shadow">
<img src="./img/threeCities.jpg" alt="Three Cities Banner" title="Three Cities" class="thumb" />
</div>
<ul class="columns">
<li class="col1">
<h3><?php
if (isset($column1Heading)) {
echo $column1Heading;
}
?></h3>
<p>
<?php
$feed = simplexml_load_file ('http://search.twitter.com/search.atom?geocode=51.5069999695%2C-0.142489999533%2C10.0mi%22london%22&lang=en&rpp=5');
if ($feed){foreach ($feed->entry as $item) {
echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
}
}
else echo "Cannot find Twitter feed!"
?>
</p>
</li>
<li class="col2">
</li>
</ul>
<div class="clear"></div>
</div>
<?php require_once 'footer.php'; ?>
You could use DomDocument Parser and DomXPath to access your link.
$dom = new DomDocument();
$dom->load('yourfeed.xml');
$xpath = new DomXPath($dom);
$links = $xpath->query('/feed/entry/link'); // returns a DomNodeList with 0 or more elements
foreach ($links as $link)
{
// $link->getAttribute('href');
}
This code is untested.
This is my first post, so I will be trying to be as thorough as possible. I am also very new to PHP.
This is a wordpress site using PODS CMS plugin so keep in mind the wordpress image uploader allows access to multiple sizes of one singular image upload. The concept is that I have a group of data called "team" and this group has three fields - images, title, bio. I will be generating a list of thumbnails for each team member in one containing unordered list and then in another containing div I will have a larger version of he image, the title, and the bio. I am basically making a tabbed content area where the thumbnails are the tabs
The ultimate HTML output would be:
<ul>
<li> <img src="thumbnailurl.jpg"/></li>
<li> <img src="thumbnailurl2.jpg"/></li>
<li> <img src="thumbnailurl3.jpg"/></li>
</ul>
<div class="panes">
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
</div>
The current issue I am having is that I can get all of the image urls for the first record, but when it comes to the second record in the second foreach i need to re-run the array for the new record but I can not figure out how.
<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
$mylist=array();
while ($Record->fetchRecord())
{
$image_array = $Record->get_field('photo');
$title = $Record->get_field('name');
$desc = $Record->get_field('bio');
$id = $Record->get_field('id');
$mylist[] = array('name' => $title, 'desc' => $desc, 'id'=> $id );
?>
<ul>
<?php
foreach($image_array as $i => $image)
{
$image_thumb_url = wp_get_attachment_image_src( $image['ID'], 'thumbnail', false );
$image_thumb_url = $image_thumb_url[0];
$image_med_url = wp_get_attachment_image_src( $image['ID'], 'medium', false );
$image_med_url = $image_med_url[0];
$image_large_url = wp_get_attachment_image_src( $image['ID'], 'large', false );
$image_large_url = $image_large_url[0];
$image_full_url = wp_get_attachment_image_src( $image['ID'], 'full', false );
$image_full_url = $image_full_url[0];
?>
<li>
<a href="<?php echo $image_large_url; ?>">
<img src="<?php echo $image_thumb_url; ?>" />
</a>
</li>
<?php } ?>
<?php } ?>
</ul>
<div class="panes">
<?php
foreach ($mylist as $person)
{ ?>
<div class="team-member" id="member<?php echo $person['id']; ?>">
<h2><?php echo $person['name']; ?></h2>
<?php echo $person['desc']; ?>
<a href="<?php echo $person['photo']; ?>">
<img src="<?php echo $person['photo']; ?>" />
</a>
<?php } ?>
</div>
</div>
Okkkay.. So i have the first problem solved!!! But it brings up a second one. I am thinking I will either need a second image field OR call just the first image in the array in the <li> and just the second image in the array for the <div>:
<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
$mylist=array();
$cnt = 0;
?>
<ul class="tabs">
<?php
while ($Record->fetchRecord()) :
$mylist[$cnt] = array(
'name' => $Record->get_field('name'),
'desc' => $Record->get_field('bio'),
'id'=> $Record->get_field('id')
);
?>
<?php
$image_array = $Record->get_field('photo');
foreach($image_array as $i => $image) :
$image_thumb_url = wp_get_attachment_image_src( $image['ID'], 'thumbnail', false );
$mylist[$cnt]['img_thumb'] = $image_thumb_url[0];
$image_med_url = wp_get_attachment_image_src( $image['ID'], 'medium', false );
$mylist[$cnt]['img_med'] = $image_med_url[0];
$image_large_url = wp_get_attachment_image_src( $image['ID'], 'large', false );
$mylist[$cnt]['img_large'] = $image_large_url[0];
$image_full_url = wp_get_attachment_image_src( $image['ID'], 'full', false );
$mylist[$cnt]['img_full'] = $image_full_url[0];
?>
<li>
<a href="#">
<img src="<?php echo $image_thumb_url[0]; ?>" />
</a>
</li>
<?php endforeach; ?>
<?php
$cnt++;
endwhile;
?> </ul>
<div class="panes">
<?php foreach ($mylist as $person) : ?>
<div class="team-member" id="member<?php echo $person['id']; ?>"><div id="member-info"><h2>Meet <?php echo $person['name']; ?></h2>
<?php echo $person['desc']; ?></div>
<a href="<?php echo $person['img_large']; ?>" rel="prettyPhoto">
<img src="<?php echo $person['img_med']; ?>" style="float:left" />
</a>
</div>
<?php endforeach; ?>
</div>
I think I can see what your problem is. Your second foreach loop doesn't benefit from the while loop, as such your only option if following this path would be to reloop through the fetched data. I wouldn't recommend doing this as there is nearly always a way you can write the code needed for the second loop into the original loop.
As an example of this I've re-written your code to incorporate the second foreach loop into the while loop (negating its need). This way, every record has a new entry into $html and $html2. Once it's looped through you have two variables that you can concat to produce the full html you are looking for.
<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
// Custom variables
$html = "<ul>\n";
$html2 = "";
$image_type=array('thumbnail','medium','large','full');
while ($Record->fetchRecord()) {
$image_array = $Record->get_field('photo');
$title = $Record->get_field('name');
$desc = $Record->get_field('bio');
$id = $Record->get_field('id');
foreach($image_array as $i => $image) {
foreach ($image_type as $type) {
$image_temp = wp_get_attachment_image_src( $image['ID'], $type , false );
$image_url[$type] = $image_temp[0];
} # End of Foreach loop
// Create List HTML in primary output variable
$html .= "<li>\n".
"<a href=\"" . $image_url['large'] ."\">\n".
"<img src=\"" . $image_url['thumbnail'] ."\" />\n".
"</a>\n".
"</li>\n";
} #End of foreach loop
// Create User HTML in secondary output variable
$html2 .= "\t<div class=\"team-member\" id=\"member" . $id . ">\n".
"<h2>" . $title . "</h2>\n".
$desc . "\n".
"<a href=\"" . $photo . ">\n". # $photo is not declared in this code (will error)
"<img src=\"" . $photo . " />\n". # as above comment
"</a>\n";
"</div>\n";
} # End of while loop
// Wrap up the list elements, concat the secondary HTML.
$html .= "</ul>\n\n".
"<div class=\"panes\">\n".
$html2 . "\n".
"</div>\n";
echo $html;
?>