I'm using the Advanced Custom Fields Repeater to pump out the image URL. But, it shows the ID, not the URL. Any help is greatly appreciated, thanks. Here is my code:
<?php
$rows = get_field('images');
foreach($rows as $row){
$image = wp_get_attachment_image_src(get_sub_field('image'), 'full');
//var_dump($row['image']);
echo '<img src="'. $row['image']['url'] . '" class="shadowed forced">';
}
?>
In response to your question, it doesn't work because get_sub_field has to be used in a while statement in conjunction with has_sub_field(), and because your $row['image']['url'] is referring to an the image object (set via return value on the field menu), while is seems as if you are actually having the field return the 'Attachment ID'.
If you want to use a foreach with the return value set to 'Attachment ID', you need to use the object/array that is being returned like so:
<?php
$rows = get_field('images');
foreach($rows as $row){
$image = wp_get_attachment_image_src($row['image'], 'full');
echo '<img src="'. $image[0] . '" class="shadowed forced">';
}
?>
If you want to use the foreach method, with the return value set to 'Image Object', it should look something like this:
<?php
$rows = get_field('images');
foreach($rows as $row){
echo '<img src="'. $row['image']['url'] . '" class="shadowed forced">';
}?>
Another solution, working of your answer/suggestion would be to set the fields return value to 'Image URL', and then you could simply call the image like so:
<?php $rows = get_field('images');
if ($rows)
{
while (has_sub_field('images'))
{
$image = get_sub_field('image');
echo '<img src="'. $image . '" class="shadowed forced">';
}
} ?>
Alternatively if you wanted to show a different size of the image, you could also set the 'Return Value' to 'Image Object', and do something like this:
<?php $rows = get_field('images');
if ($rows)
{
while (has_sub_field('images'))
{
$image = get_sub_field('image');
echo '<img src="'. $image['sizes']['large'] . '" class="shadowed forced">';
}
} ?>
Looks like what I needed to do was not use a foreach, and instead just use and if & while statement. I was calling in the repeater, not the subfield with the old code. Here's a reference if anyone runs into this issue -
<?php
$rows = get_field('images');
if ($rows)
{
while (has_sub_field('images'))
{
$image = wp_get_attachment_image_src(get_sub_field('image'), 'full');
echo '<img src="'. $image[0] . '" class="shadowed forced">';
}
}
?>
Related
I have a list of image URLs stored in a database, separated by line breaks. The following PHP code displays them on my site:
$images = explode("\n", $value );
$value = '';
foreach ($images as $im){
$value .= '<img src="'. $im . '" style="max-width:1024px"/> <br />';
}
However, I want to hide some of the images behind a collapsible div if they contain a particular string.
Is it possible to write an if statement so $value changes to some HTML code displaying a collapsible div?
For example:
Rather than $value:
<img src="'. www.example.com/hiddenimage1.jpg . '" style="max-width:1024px"/> <br />
It would become:
<div class="collapsiblediv" style="etc etc"><img src="'. www.example.com/hiddenimage1.jpg . '" style="max-width:1024px"/> <br /></div>
However anything NOT containing "hiddenimage" would display as usual.
Is it possible?
You can use the strstr() function of PHP to search for a chunk of a string within a string.
Using this function, you can search for the value 'hiddenimage' within your foreach loop. Based on a conditional, you could then return either the collapsible <div> or the <img>
You could try this code
$images = explode("\n", $value );
$value = '';
foreach ($images as $im){
if(strstr( $im, 'hiddenimage' ) {
<div class="collapsiblediv" style="etc etc"><img src="'.
$im . '" style="max-width:1024px"/> <br
/></div>
} else {
$value .= '<img src="'. $im . '" style="max-width:1024px"/> <br />';
}
}
I've created a simple function that outputs an image with the correct html markup, seen below. The problem I am facing is when I pass into the alt text that has a space in it, such as "My cat is great", the alt breaks and shows alt="My" like <img src="blah.jpg" alt="My" cat is great class="home">. I'm having a hard time trying to figure this out. Any thoughts?
function image_creator($image_url, $alt=false, $class=false) {
$string = '<img src='.$image_url.' alt='.$alt.' class='.$class.'>';
return $string;
}
You're not actually outputting <img src="blah.jpg" alt="My" cat is great class="home"> - that's just how the browser is interpreting it.
You're outputting <img src=blah.jpg alt=My cat is great class=home>
You need to output some quotes:
$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
$string = "<img src='".$image_url."' alt='".$alt."' class='".$class."'>';
try this i think it will work
A little user failure. No problem!
Make sure you have your <img> tag correctly.
$image_url = 'https://example.com/image';
$alt = 'My cat is awesome';
$class = 'image';
So:
function image_creator($image_url, $alt=false, $class=false) {
$string = '<img src='.$image_url.' alt='.$alt.' class='.$class.'>';
return $string;
}
Outcome:
<img src="https://example.com/image" alt="My" cat="" is="" awesome="" class="image">
Should be:
function image_creator($image_url, $alt=false, $class=false) {
$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
return $string;
}
Outcome:
<img src="https://example.com/image" alt="My cat is awesome" class="image">
Do you see the differences? Look carefully at the quotes.
Documentation:
http://php.net/manual/en/language.types.string.php
Some examples about using quotes:
https://www.virendrachandak.com/techtalk/php-double-quotes-vs-single-quotes/
Personally I would like to use:
// $image below is being sent to the function
$image = [
'image_url' => 'https://example.com/image',
'alt' => 'My cat is awesome',
'class' => 'image',
];
function image_creator($image = [])
{
if(empty($image))
{
return 'No image';
}
return '<img src="' . $image['image_url'] . '" alt="' . $image['alt'] . '" class="' . $image['class'] . '">';
}
//Multiple solution with multiple way
$image_url = 'https://example.com/image';
$alt = 'My cat is awesome';
$class = 'image';
$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
//also you can direct echo
echo '<img src="',$image_url,'" alt="',$alt,'" class="',$class,'">';
//also you can direct echo
$string= "<img src='{$image_url}' alt='{$alt}' class='{$class}' >";
//also you can direct echo
$string = "<img src='${image_url}' alt='${alt}' class='${class}' >";
echo $string;
What I'm trying to do is get the photo_id='.$pid.' of each photo for the link wrapped around them inside the foreach loop.
I tried $image['photo_id'] but that didn't work it just printed image symbols. I then wrapped a separate foreach loop around the current one and that duplicated the amount of images. I'm new to using the foreach loop with arrays and keys so still trying to get my head around it.
$images = array();
while ($rowhhh = mysqli_fetch_assoc($resulthhh)) {
$images[] = $rowhhh['photo_imagedata'];
$photo_id[] = $rowhhh['photo_id'];
}
foreach ($images as $image) {
if($photo_num==1){
echo '<img class="stream_images" style="width:200px;height:200px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image) .'" />';
}else{
echo '<img class="stream_images" style="width:100px;height:100px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image) .'" />';
}
}
Personally, I'd use the $images var as a multidimensional array to store the ID and the Data of each image. Then, you can access both of these bits of information easily in your foreach() loop.
$images = array();
while ($rowhhh = mysqli_fetch_assoc($resulthhh)) {
$imageArray = array(
'data' => $rowhhh['photo_imagedata'],
'id' => $rowhhh['photo_id']
);
$images[] = $imageArray;
}
foreach ($images as $image) {
if($photo_num==1){
echo '<img class="stream_images" style="width:200px;height:200px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image['data']) .'" />';
}else{
echo '<img class="stream_images" style="width:100px;height:100px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image['data']) .'" />';
}
}
I'm stuck on this for a while now: I'm trying to combine two arrays, one is every image in a folder, and the other one is the description of the picture from the SQL Database. In the database I've added the filename and the apartment name. To make my query work I need it to be in a loop, but to show the results of my query, it needs to become a loop, so I would think it must be possible to combine it right? But I can't seem to figure out how to combine these two arrays in a loop. I've tried with two loops and this is what I've got:
<?php
$directory = "images/photos/";
$images = glob('images/photos/*.{jpg,png,gif}', GLOB_BRACE);
foreach ($images as $image) {
$sqlaa = "SELECT * FROM `afbeelding` WHERE filename = ' . $image . '";
$titles = mysqli_query($link, $sqlaa);
while ($row = $titles->fetch_array()) {
echo '<form action="" method="post"><li>
<a href="' . $image . '" title="' . $row['apartment'] . '" >
<img style="width:150px;height:150px;" src="' . $image . '" />
</a>
</li>';
}
}
?>
If anyone knew, that'd be great!
Thanks in advance
You can try making the loop over the data and then check the image in the loop, this could be faster and avoids using a query inside a loop.
<?php
$directory = "images/photos/";
//$images = glob('images/photos/*.{jpg,png,gif}', GLOB_BRACE);
$sqlaa = "SELECT filename,apartment FROM `afbeelding`";
$titles = mysqli_query($link, $sqlaa);
while($row = $titles->fetch_array())
{
$image=$row["filename"];
if (file_exists($directory.$image)){
echo '<form action="" method="post"><li>
<a href="'.$image.'" title="' . $row['apartment'] . '" >
<img style="width:150px;height:150px;" src="'.$image.'" />
</a>
</li>';
}
}
?>
I don't know if the image is stored using the full relative path "images/photos/img.png" or the base filename only "img.png".
If the data in afbeelding is huge, you can improve the sql query using an IN filter
function imageNames($item){
return "'$item'";
}
$images = glob('images/photos/*.{jpg,png,gif}', GLOB_BRACE);
$names=array_map("imageNames",$images);
$filter_names=implode(",",$names);
$sqlaa = "SELECT filename,apartment FROM `afbeelding` WHERE filename IN ($filter_names)";
And then proceed with the loop, without checking files.
How about... going about it differently.
You already have the filenames in database.
So just poll your database with your available data, and then just echo it.
I'm assuiming it's stored as images/photos/filename.jpg in your database.
But without a data sample as is in your database I can't speculate more.
$directory = "images/photos/";
foreach ($images as $image) {
$sqlaa = "SELECT * FROM `afbeelding` WHERE filename like '%images/photos/%'";
$titles = mysqli_query($link, $sqlaa);
while ($row = $titles->fetch_array()) {
echo '<form action="" method="post"><li>
<a href="' . $image . '" title="' . $row['apartment'] . '" >
<img style="width:150px;height:150px;" src="' . $row['filename'] . '" />
</a>
</li>';
}
}
first: loops on db-calls are not really best practice..
A possible result to your question:
<?php
$directory = "images/photos/";
$images = glob('images/photos/*.{jpg,png,gif}', GLOB_BRACE);
$images = array_flip($images);
// get data first
foreach($images as $image => $v) {
$sqlaa = "SELECT * FROM `afbeelding` WHERE filename = ' . $image . '";
$titles = mysqli_query($link, $sqlaa);
if ($title->num_rows == 1) {
$row = $titles->fetch_array();
$images[$image] = $row['apartment'];
} else {
$images[$image] = '';
}
}
// render data
foreach ($images as $filename => $description) {
echo '<form action="" method="post"><li>
<a href="'.$filename.'" title="' . $description . '" >
<img style="width:150px;height:150px;" src="'.$filename.'" />
</a>
</li>';
}
?>
Not really nice, but plain simple..
<?php
$feedURL = 'http://########.tumblr.com/api/read/';
$xml = simplexml_load_file($feedURL);
// $posts = $xml->xpath("/tumblr/posts/post");
foreach($xml->posts->post as $post){
$post= $xml->posts->post->{'photo-url'};
}
echo "<pre>";
print_r($post);
echo "</pre>";
?>
This is the script that I have made. I am able to fetch only the first post, but I want all posts to be displayed and the post image is not showing up.
I've got it working using
foreach($xml->posts->post as $post){
$img = (string) $post->{'photo-url'};
echo '<img src="' . $img . '" />';
}
But its only showing a couple image not all of them any ideas ?
Try this:
foreach($xml->posts->post as $post){
$post_urls[] = (string) $post->{'photo-url'};
}
echo "<pre>";
print_r($post_urls);
echo "</pre>";
Update for comment:
foreach($xml->posts->post as $post){
$posts[] = (array)$post->attributes() + (array) $post->children();
}
echo "<pre>";
print_r($posts);
echo "</pre>";
Update 2:
foreach($xml->posts->post as $post){
$img = (string) $post->{'photo-url'};
echo '<img src="' . $img . '" />';
}