Hello I need some help getting the image URL of the itunes:image attribute from my XML rss feed. I can retrieve everything else in the array, but not 'mainimg'. The current code returns nothing for the image although it's there. Do you have any idea how I can do this, I'm looking for the HREF so I can place it inside 'img' tags.
<?php
$doc = new DOMDocument();
$doc->load('http://rss.acast.com/globalpillage');
$cnt=0;
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'maintitle' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'enclosure' => $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url'),
'mainimg' => $node->getElementsByTagName('image')->item(0)->getAttribute('url')/*This is what I'm trying to call the itunes:image attribute(HREF) with*/,
);
?>
<div class="showcontent">
<img src="<?php echo $itemRSS['image']; /*echo the itunes:image attribute(HREF)*/ ?>">
<h2><?php echo $itemRSS['title']; ?></h2>
<p><strong>Published</strong> <?php echo $itemRSS['date']; ?></p>
<audio controls>
<source src="<?php echo $itemRSS['enclosure']; ?>" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</div>
<?php $cnt ++; } ?>
// Get namespace URI for prefix `itunes`
echo $ns = $s->lookupNamespaceURI('itunes');
// Get `image` tag from that namespace
echo $href = $node->getElementsByTagNameNS($ns, 'image')->item(0)->getAttribute('href');
If anyone stumbles upon this, I could not get the comment nor the answer to work. I think the comment should have $doc not $dom to mimic the example code, but that resulted in just the image for the podcast each time, not the "items". It looked like this:
echo $href = $doc->getElementsByTagNameNS('http://www.itunes.com/dtds/podcast-1.0.dtd', 'image')->item(0)->getAttribute('href');
But when I changed to $node in the loop, I received the correct image:
echo $href = $node->getElementsByTagNameNS('http://www.itunes.com/dtds/podcast-1.0.dtd', 'image')->item(0)->getAttribute('href');
Also, there are a few other typos I came across in the example code like storing $item['maintitle'] but echoing $item['title']
I could never get the accepted answer to work. In the end, this is what my code looked like this when I was scraping an Anchor podcast RSS feed:
<?php
$doc = new DOMDocument();
$doc->load('https://anchor.fm/s/xxxxxxx/podcast/rss');
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'enclosure' => $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url'),
'mainimg' => $node->getElementsByTagNameNS('http://www.itunes.com/dtds/podcast-1.0.dtd', 'image')->item(0)->getAttribute('href')
);
?>
<div class="showcontent">
<img src="<?php echo $itemRSS['mainimg']; ?>">
<h2><?php echo $itemRSS['title']; ?></h2>
<p><strong>Published</strong> <?php echo $itemRSS['date']; ?></p>
<audio controls>
<source src="<?php echo $itemRSS['enclosure']; ?>" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</div>
<?php } ?>
Related
i am trying to display a list of image from some link within a array of hashes. beside some other image from my own folder. Thanks in advance
$config = [
'gallery_name' => 'my_heading',
'unsplash_categories' => ['array','of','category','keywords'],
'local_images' =>
['folder/jpg1.jpg','folder/jpg2.jpg','folder/jpg1.jpg','folder/jpg4.jpg']
];
in HTML, what tried.
<?php foreach($config as $my_config => $gallery_name): ?>
<h1><?php echo $gallery_name; ?></h1>
<?php endforeach ?>
<p>
<?php foreach($config as $img)
{
foreach($img['local_images'] as $local_img)
{
echo "<img src='".$local_img['']."' alt=''>.<br>";
}
foreach($img['unsplash_categories'] as $unsplash_img)
{
echo "<img src='".$unsplash_img['']."' alt=''>.<br>";
}
}
?>
</p>
Welcome on SO, Shaik.
Not clear on what you want to exactly achieve but if you just want to display the images using your $config as sample, you can simplify it a bit to something like this:
<h1><?= $config["gallery_name"]; ?></h1>
<p>
<?php
foreach($config['local_images'] as $local_img)
{
echo "<img src='" . $local_img . "' alt=''>.<br>";
}
//same for unsplash_categories...
?>
</p>
I have a foreach loop giving out the results of a landscape gallery images.
Basically starting with
<?php foreach ( $images as $image ) : ?>
and then the regular div + anchor + image tags for the gallery images.
<?php if ( $image->description == "portrait" ) : ?>
<div id="ngg-image-<?php echo $image->pid ?>" class="ngg-gallery-thumbnail-box portrait" <?php echo $image->style ?> >
<div class="ngg-gallery-thumbnail" >
<a class="portrait-image" href="<?php echo $image->imageURL ?>" title=" " id="<?php echo $image->alttext ?>" rel="lightbox" <?php echo $image->thumbcode ?> >
<img title="<?php echo ' ' /* $image->alttext */ ?>" alt="<?php echo ' ' /* $image->alttext */ ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
</a>
<?php else: ?>
<div id="ngg-image-<?php echo $image->pid ?>" class="ngg-gallery-thumbnail-box landscape" <?php echo $image->style ?> >
<div class="ngg-gallery-thumbnail" >
<a class="landscape-image" href="<?php echo $image->imageURL ?>" title=" " id="<?php echo $image->alttext ?>" rel="lightbox" <?php echo $image->thumbcode ?> >
<img title="<?php echo ' ' /* $image->alttext */ ?>" alt="<?php echo ' ' /* $image->alttext */ ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
</a>
<?php endif; ?>
I want to have special case, where I have two portrait images side-by-side, filling the space of one landscape image.
What I was thinking about is to have a tag in the admin area which would trigger the break out of the foreach loop to have two images in one container div, and then it would continue the regular loop just with the container + image for the landscape images.
Is it possible in the case of foreach loop, to jump out of the loop, make a case with different settings, and then return to the regular looping method?
You can try something like this:
foreach($images as $key => $image) {
if($image has special tag) { // <--- PSEUDOCODE
echo "<div class='SPECIAL_CSS_CLASS'><img src='" . $img->path . "' /></div>";
}
}
Then in your CSS just do your Magic!
I would go ahead and do something like this:
//Function to get all the images based on a specific portrait_group
function getImagesPortraitGroup($group, $images) {
$result = array();
foreach ($images as $image) {
if (isset($image['portrait_group']) && $image['portrait_group'] == $group) {
$result[] = $image;
}
}
return $result;
}
//The images to show
$images = array(
["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image1", "portrait_group" => 1],
["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image2", "portrait_group" => 1],
["url" => "http://lorempixel.com/800/200/", "caption" => "Lorem Image3"],
["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image4", "portrait_group" => 2],
["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image5", "portrait_group" => 2],
);
//Everytime we show a group, we track it by adding it to this array in order not to show it again
$addedGroups = array();
//Loop through all the images
for ($i = 0; $i < count($images); $i++) {
echo "<div>";
//If the image has a portrait_group that is not already shown, show it
if (isset($images[$i]['portrait_group']) && !in_array($images[$i]['portrait_group'], $addedGroups)) {
$groupImages = getImagesPortraitGroup($images[$i]['portrait_group'], $images);
foreach ($groupImages as $image) {
echo "<img src='{$image['url']}' title='{$image['caption']}' >";
}
//Save the group to the array in order not to show it again
$addedGroups[] = $images[$i]['portrait_group'];
} else {
echo "<img src='{$images[$i]['url']}' title='{$images[$i]['caption']}' >";
}
echo "</div>";
}
I managed to make the grouping by jQuery, this is the code
jQuery(".ngg-gallery-thumbnail-box.portrait").filter(":odd").each(function(){
$content = jQuery(this).find("a");
jQuery(this).prev().find(".ngg-gallery-thumbnail").append($content);
jQuery(this).remove();
});
I have a block of PHP that is talking to my RSS feed and importing it. The problem is, it's decided to return a 500 Internal Server Error and I can't seem to figure out why. I've used this code with a previous feed and it worked perfectly. But with this new feed it breaks. Any ideas?
Thanks in advance!
<?php
$doc = new DOMDocument();
$doc->load('http://guiltyfeminist.com/rss');
$cnt=0;
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'maintitle' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'enclosure' => $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url'),
);
?>
<div class="showcontent">
<div class="contentleft">
<img src="<?php echo $node->getElementsByTagNameNS('http://www.itunes.com/dtds/podcast-1.0.dtd', 'image')->item(0)->getAttribute('href'); ?>">
</div>
<div class="contentright">
<h2><?php echo $itemRSS['maintitle']; ?></h2>
<p><strong>Duration</strong> <?php echo $node->getElementsByTagNameNS('http://www.itunes.com/dtds/podcast-1.0.dtd', 'duration')->item(0)->nodeValue; ?></p>
<p><strong>Published</strong> <?php echo $itemRSS['date']; ?></p>
<audio controls>
<source src="<?php echo $itemRSS['enclosure']; ?>" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</div>
</div>
<?php $cnt ++; } ?>
your code is working fine. you can try same code on another PC
I get images in a array then i read it with a foreach this are images, but how can i get also image text, my example is here:
$slideImages = array('image/path/image1', 'image/path/image2', 'image/path/image13');
$slideText = array('image text 1', 'image text 2', 'image text 3');
if($slideImages) :
echo '<ul>';
foreach($slideImages as $slideImage) :
?>
<li>
<div>IMAGE TEXT</div>
<img src="<?php echo $slideImage; ?>" />
</li>
<?php
endforeach;
echo '</ul>';
endif;
I need to embed also image text from the true position, i know how to do it with for but so is not efficient... it's something like this possible?
Assuming the keys are the same, use the $key in the foreach:
foreach($slideImages as $key => $slideImage) :
?>
<li>
<div>IMAGE TEXT</div>
<img src="<?php echo $slideImage; ?>" />
<?php echo $slideText[$key]; ?>
</li>
<?php
endforeach;
You could also change the way you store the info and keep them in one multidimensional array.
$slides = array(
array( image => "image/path/image1", text => "image text 1"),
array( image => "image/path/image2", text => "image text 2"),
array( image => "image/path/image3", text => "image text 3")
);
Then you would iterate through the $slides array and just pull out the image and text for each:
foreach($slides as $slide)
{
echo "Text:".$slide["text"];
echo "Image Path:".$slide["image"];
}
This organizes your code a little better so you don't wind up with something crazy going on should your two arrays wind up out of sync.
simplified example:
foreach($slideImages as $key=>$var){
echo $var;
echo $slideText[$key];
}
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;
?>