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.
Related
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
**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; ?>" />
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;
?>
I have this custom search function to search trough custom meta keys in Wordpress. The function works well.
However, all the images that I have added to my post using Advanced Custom Fields are now having their URL's changed to the attachment ID.
This should not happen according to the settings page in ACF:
The same field works ok on other pages, just not on the search results page. Check how it changes the image source on the search results:
How and why is the image URL here changed to the attachment ID? Kindly check out my code below:
function custom_search_function($pieces) {
// filter to select search query
if (is_search() && !is_admin()) {
global $wpdb;
$custom_fields = array('regio','provincie');
$keywords = explode(' ', get_query_var('s'));
$query = "";
foreach ($custom_fields as $field) {
foreach ($keywords as $word) {
$query .= "((mypm1.meta_key = '".$field."')";
$query .= " AND (mypm1.meta_value LIKE '%{$word}%')) OR ";
}
}
if (!empty($query)) {
// add to where clause
$pieces['where'] = str_replace("((({$wpdb->posts}.post_title LIKE '%", "( {$query} (({$wpdb->posts}.post_title LIKE '%", $pieces['where']);
$pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
//$pieces['groupby'] = "{$wpdb->posts}.ID";
}
}
return ($pieces);
}
add_filter('posts_clauses', 'custom_search_function', 20, 1);
EDIT: Here's the code that displays my post results, the "foto" field is the field which is responsible for displaying the image:
<?php foreach( $posts as $post ):
//fusion-column-last, or none for normal class
$lastclass = '';
if(++$counter % 2 === 0) {
$lastclass = ' fusion-column-last';
}
setup_postdata( $post )
?>
<div class="fusion-one-half fusion-layout-column fusion-spacing-yes<?php echo $lastclass?>" style="margin-top:0px;margin-bottom:20px;background-color:white;">
<div class="fusion-column-wrapper">
<div class="bw-search-picture">
<?php $postid = get_the_ID(); ?>
<?php //echo $postid; ?>
<img src="<?php the_field('foto', $postid); ?>" alt="<?php the_title(); ?>"/>
</div>
<div class="bw-search-content">
<h2>
<a class="green" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h2>
<p class="bw-regio">Regio <?php the_field('regio'); ?></p>
<p>
<a style="color:#9C9E9F;" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">LEES VERDER ></a>
</p>
</div>
</div>
</div>
<?php endforeach; ?>
Try for the thumbnail:
$imgsrc = wp_get_attachment_image_src(get_post_thumbnail_id( $post_id ));
echo $imgsrc[0];
Edit, use this code with the attachment id:
$imgsrc = wp_get_attachment_image_src(get_field('foto', $postid));
echo $imgsrc[0];
Rather than using
<img src="<?php the_field('foto', $postid); ?>" />
Try using
<?php $foto_url = get_field('foto', $postid); ?>
<img src="<?php echo $foto_url; ?>" />
I am trying to display RSS feed in my magento website and I'm having difficulties showing the image for the feed. Studying the feed, I see that the image is inside a content:encoded tag so I can't access it directly by using something like $item->image. Here's my current code:
<?php $channel = new Zend_Feed_Rss('newsfeedurl'); ?>
<?php foreach ($channel as $item): ?>
<?php if($i<2) { ?>
<img src="<?php echo $item->image; ?>" title="<?php echo $item->title; ?>" height="63" width="95" />
<?php echo "image:".$item->content; ?>
<?php } else {} ?>
<?php $i++; ?>
<?php endforeach; ?>
$?>
I tried also tried using $item->content but this returns the entire content of the newsfeed. So my question is, how can I access the image's source from content:encoded in order to display it on my feed?
UPDATE: After some more research, I tried using preg_match like so: preg_match('/<*img[^>]*src = ["\']?([^"\'])/i', $item->content, $matches); echo $matches[0]; I'm getting the correct image path but I've placed this inside a loop so each I should have at least 2 images but I'm only getting 1. why is this?
SOLVED: I've managed to solve my problem by changing $matches[0] to $matches[1]. I guess I was using 0 thinking it was the index of an array matches.
In order to get the image source from the content:encoded tag, I used regular expression (preg_match). Here's how my current code looks:
<?php $channel = new Zend_Feed_Rss('newsfeedurl'); ?>
<?php foreach ($channel as $item): ?>
<?php preg_match('/<*img[^>]*src *= *["\']?([^"\']*)/i', $item->content, $matches); ?>
<?php if($i<2) { ?>
<img src="<?php echo $matches[1]; ?>" title="<?php echo $item->title; ?>" height="63" width="95" /></a></div>
<?php } else {} ?>
<?php $i++; ?>
<?php endforeach; ?>
Hope this helps someone else.