how to access meta value under image link in wordpres - php

**meta_value**:a:1:{i:0;s:105:"http://localhost/wordpress/wp-content/uploads/event-manager-uploads/event_banner/2020/07/diabetic_1-3.jpg";}
meta_key:_event_banner
while($res = mysqli_fetch_array($query))
{
$i++;
// $img_src=wp_get_attachment_image_src(get_post_thumbnail_id($res['image']));
// $img_src_url=$img_src[0];
$id=$res['post_id'];
$post = get_post($res['post_id'] );
?>
<div class="maindiv">
<div class="notification">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($id) );?>"></td>
<img src="<?php echo $post->_event_banner;?>"></td>
</div>
<div class="notification1">
<h2><?php echo $res['post_title']?></h2>
<h6><?php echo $res['date']?></h6>
</div>
</div>
<?php
}
how to display image give me some reference I need help on how to access the image in meta key use and filter I have not idea in meta key use give me idea... it's very important

So not sure how you're saving your meta_key in the first place? It looks like what you have is a serialized array with just a single, numerically indexed entry. So what you probably want to do is change wherever that's saved to just store the URL?
That said, you could access that meta value like this:
$banner_src = maybe_unserialize( get_post_meta( $post->ID, '_event_banner', true ) );
$banner_src = is_array( $banner_src ) ? $banner_src[0] : $banner_src;
And then display the banner like:
<img src="<?php echo $banner_src; ?>" />

Related

How do I get ACF image alt text?

I have a wordpress project that is using ACF fields to pass images/video into a carousel. How would I get the alt text for the associated image?
I have tried to get_field('image') and various get_sub_field() calls, but image does not seem to be a field even though get_sub_field('still_image_url') and get_sub_field('image_link') are already pulling in the respective data for those fields.
I'm not even sure how to get the id for the image. Another php file is using the_ID();, but that is not working here.
<?php while (have_rows('top_slider')) : the_row(); ?>
<?php
$video_url = get_sub_field('video_url');
$video_url_responsive = get_sub_field('video_url_responsive');
$video_link = get_sub_field('video_link');
$image_url = get_sub_field('still_image_url');
$image_link = get_sub_field('image_link');
$has_target = strpos($image_link, '/') !== 0;
?>
Make sure you are using the return format of the Image either as IMAGE ARRAY or IMAGE ID.
Use the below code to get the ALT tag of the image if the return format is IMAGE ARRAY.
<?php
$image =get_sub_field('image');
if( !empty($image )): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
Use the below code to get the ALT tag of the image if the return format is IMAGE ID.
$image_id = get_sub_field('image');
$img_url = wp_get_attachment_image_src($image_id, 'full');
$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE);
$image_title = get_the_title($image_id);
?>
<img src="<?php echo $img_url[0]; ?>" width="<?php echo $img_url[1]; ?>" height="<?php echo $img_url[2]; ?>" alt="<?php echo $image_alt; ?>" title="<?php echo $image_title; ?>">
Here the "image" denotes the field name which you set while creating the field.
get_sub_field('image');
Refer the image for understanding about Field Name, Return Format etc
You can see the image id and other details on the REST API if you will set the options to show on REST API.
After adding the contents to WP REST API, you can display the pages' content on the WP REST API as you can use this example link:
https://write-your-site-url-here/wp-json/wp/v2/pages

Problem with getting count with Wordpress

I am trying to display wordpress image using it ID but the code i use is giving an error which is
Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/wordpress/wp-content/themes/monstajamss/template-parts/content-article.php on line 10
This is my code
<div class="image large">
<?php $thumb_id = get_post_thumbnail_id(get_the_ID()); $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true); if(count($alt)) ?>
<img src="<?php the_post_thumbnail_url('large-size'); ?>" alt="<?php echo $alt; ?>">
</div>
You are just using get_post_meta incorrectly - it returns an array by default, but you are passing in true as the third parameter which changes it to return a single value only - see the Code Reference for get_post_meta.
Therefore your return value is not array, so you don't need to try count (which only works with arrays) or anything to evaluate an array of values before using it - it already has a single value so you just need to do the following:
<div class="image large">
<?php $thumb_id = get_post_thumbnail_id(get_the_ID());
$alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
?>
<img src="<?php the_post_thumbnail_url('large-size'); ?>"
<?php
// only include the alt attrib if the alt text is not empty
// (or you could set a default value, or whatever else you might want to do)
if ($alt): ?>
alt="<?php echo $alt; ?>"
<?php endif; ?>
/>
</div>

Rule to hide DIV if no image?

Hope you're well.
I have got the below code working as intended, but is there a way of ONLY showing the div 'listinggallery' if there are images returned?
At the moment, it works great if there are images in the listing, but if there are no images, then I have an empty styled div showing. Ideally I want create a rule to say "IF listingimage 'true' then show 'listinggallery'".
I have tried placing the 'listinggallery' div elsewhere within the code but just seems to crash my site, so hoping I can create a rule?
Kind regards,
Spencer
<div class="listinggallery">
<?php
//Get the images ids from the post_metadata
$images = acf_photo_gallery('gallery', $post->ID);
//Check if return array has anything in it
if( count($images) ):
//Cool, we got some data so now let's loop over it
foreach($images as $image):
$id = $image['id']; // The attachment id of the media
$full_image_url= $image['full_image_url']; //Full size image url
$full_image_url = acf_photo_gallery_resize_image($full_image_url, 1024, 768); //Resized size to 262px width by 160px height image url
$thumbnail_image_url= $image['thumbnail_image_url']; //Get the thumbnail size image url 150px by 150px
$url= $image['url']; //Goto any link when clicked
$target= $image['target']; //Open normal or new tab
$alt = get_field('photo_gallery_alt', $id); //Get the alt which is a extra field (See below how to add extra fields)
$class = get_field('photo_gallery_class', $id); //Get the class which is a extra field (See below how to add extra fields)
?>
<div class="listingimage">
<div class="thumbnail">
<?php if( !empty($url) ){ ?><a href="<?php echo $url; ?>" <?php echo ($target == 'true' )? 'target="_blank"': ''; ?>><?php } ?>
<a href="<?php echo $full_image_url; ?>" class="fancybox">
<img src="<?php echo $thumbnail_image_url; ?>" alt="<?php echo $title; ?>" title="<?php echo $title; ?>">
</a>
<?php if( !empty($url) ){ ?></a><?php } ?>
</div>
</div>
<?php endforeach; endif; ?>
</div>
If you move the creation of the <div> inside the block which decides if there is anything to display...
<?php
$images = acf_photo_gallery('gallery', $post->ID);
//Check if return array has anything in it
if( count($images) ):
// Output start of gallery div
?>
<div class="listinggallery">
<?php
//Cool, we got some data so now let's loop over it
foreach($images as $image):
// rest of code as it currently is
endforeach;
// Close of gallery div
?>
</div>
<?php
endif;
?>

How to loop within a for loop to get multiple values with the same ID

I want to get some data from a MySql database which has the same ID but different values. see the image (this is just a sample)
Although the venue styles are different, I want to pull all the styles with the same ID. I'm using a foreach loop to get the data from the database.
How can I improve my code to achieve what I want.
<?php
$myrows = $wpdb->get_results( "SELECT vf_venues.title, vf_venues.mainimage,
vf_venues.permalink, vf_venuestyles.slug
FROM vf_venues
LEFT JOIN vf_venuestyles ON vf_venuestyles.vid=vf_venues.vid WHERE
vf_venuestyles.vid=vf_venues.vid" );?>
<div class="venue-list venue-grid">
<?php
foreach ( $myrows as $myrow ) {
//pull the data from the DB
"<pre>"
$venueName = $myrow->title;
$mainImage = $myrow->mainimage;
$permalink = $myrow->permalink;
$slug = $myrow->slug;
$vid = $myrow->vid;
"<pre>"
?>
<li class="venue-block block">
<div class="venue-img">
<a href="<?php echo $permalink; ?>">
<img src="<?php echo $mainImage; ?>">
</a>
</div>
<div class="venue-details"><h2><?php echo $venueName; ?></h2></div>
<?php echo $slug; ?>
<?php echo $vid; ?>
</li>
<?php
}
?>
</div>
I managed to fix this by creating a for loop within the existing for loop. I then created a sql query to pull the venue styles for that venue.

How to get all external image in wp post

I did not use attachment to upload image in wp post,
I use external host like dropbox, how to get it to my frontpage by specific post-type ?
example
<img src="https://www.dropbox.com/s/drgdrfhdtj.jpg?raw=1" alt="" width="506" height="673" />
<img src="https://www.dropbox.com/s/djtfdtjdtmjx.jpg?raw=1" alt="" width="506" height="714" />
how to get all this image ?
my code in function.php
function gallery (){
$pid = get_the_ID();
$post = get_post( $pid );
$content = $post->post_content;
$regex = '/src="([^"]*)"/';
preg_match_all( $regex, $content, $matches );
foreach($matches as $image):
echo '<img src="'.$image.'" alt="'.$post->post_title.'" title="'.$post->post_title.'">' ;
endforeach;
}
my code in content-gallery.php
<?php $post_format = get_post_format(); ?>
<?php if ( $post_format == 'gallery' ) : ?>
<div class="featured-media">
<?php gallery(); ?>
</div> <!-- /featured-media -->
<?php endif; ?>
but this do not work
edit:
work used image[1] (#master djon answer)
now problem is
output:
<img src="https://www.dropbox.com/s/image1.jpg""><img https://www.dropbox.com/s/image1.jpg">
double output one image <img src= and <img https://
how fix this ?
$image is an array of all groups contained in found match. Index zero is the whole matching (src="URL"), and index 1 is what you search for : the URL.
So you shall you $image[1] instead of $image.

Categories