How to pass a parameter in a variable - php

I'm using ACF within a WordPress theme.
<div class="hero">
<?php
$landscape_image_1x = wp_get_attachment_image_url( get_field('landscape_image'), 'hero-landscape-1x' );
$landscape_image_2x = wp_get_attachment_image_url( get_field('landscape_image'), 'hero-landscape-2x' );
$portrait_image_1x = wp_get_attachment_image_url( get_field('portrait_image'), 'hero-portrait-1x' );
$portrait_image_2x = wp_get_attachment_image_url( get_field('portrait_image'), 'hero-portrait-2x' );
$image_alt = get_post_meta( $hero_landscape, '_wp_attachment_image_alt', true);
?>
<picture>
<source
media="(orientation: landscape)"
srcset="
<?= $landscape_image_1x; ?> 1x,
<?= $landscape_image_2x; ?> 2x"
>
<source
media="(orientation: portrait)"
srcset="
<?= $portrait_image_1x; ?> 1x,
<?= $portrait_image_2x; ?> 2x"
>
<img src="<?= $landscape_image; ?>" alt="<?= $image_alt; ?>">
</picture>
</div>
The first code block works perfectly.
At the moment, I'm repeating a lot of code. All that's changing for each variable is the field name e.g. landscape_image and the size parameter e.g. hero-landscape-1x.
How within PHP do you pass values when echoing variables? Here's my very wrong way of explaining what I mean.
<?php
$image = wp_get_attachment_image_url( get_field( $field ), $parameter );
echo $image('landscape_image', 'hero_landscape_1x');
echo $image('landscape_image', 'hero_landscape_2x');
<?

If I understand correctly, you’re annoyed with the verbosity of
<?php
$landscape_image_1x = wp_get_attachment_image_url( get_field('landscape_image'), 'hero-landscape-1x' );
$landscape_image_2x = wp_get_attachment_image_url( get_field('landscape_image'), 'hero-landscape-2x' );
$portrait_image_1x = wp_get_attachment_image_url( get_field('portrait_image'), 'hero-portrait-1x' );
$portrait_image_2x = wp_get_attachment_image_url( get_field('portrait_image'), 'hero-portrait-2x' );
$image_alt = get_post_meta( $hero_landscape, '_wp_attachment_image_alt', true);
?>
There are a few ways you could go; I see repetition of get_field(), but even more than that I see repetition of wp_get_attachment_image_url(get_field())
TBH, aside from the loooong function names, this isn’t a big deal unless you find yourself repeating this block.
So one option is to assign repeated values, such as
$landscape = get_field('landscape_image');
$portrait = get_field('portrait_image');
but that doesn’t really help much.
Another idea is to encapsulate getting the url with a function. Notice that you can eliminate the one-time use variables.
function image_url($orientation, $location) {
return wp_get_attachment_image_url( get_field($orientation), $location );
}
?>
<picture>
<source
media="(orientation: landscape)"
srcset="<?= image_url('landscape_image', 'hero-landscape-1x' ?>" 1x,
<?= image_url('landscape_image','landscape_image_2x') ?> 2x"
>
It’s still kinda wordy, but you have eliminated unnecessary assignments while at the same time remaining descriptive in the view.
If you’re worried about collision with existing function name, you could also make it a closure:
$url = function ($orientation, $location) {
return wp_get_attachment_image_url( get_field($orientation), $location );
}
?>
<picture>
<source
media="(orientation: landscape)"
srcset="<?= $url('landscape_image', 'hero-landscape-1x' ?>" 1x,
<?= $url('landscape_image','landscape_image_2x') ?> 2x"
>

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>

how to access meta value under image link in wordpres

**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; ?>" />

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.

How do I resize a wordpress featured image to be 620 width by whatever corresponding height in the loop but not permanently?

Here is the website I am attempting this on: http://increaseinwebtraffic.com/marywood/deals/
The top few deals are larger than 620 width, but the bottom ones are smaller. I tried to use the code below with no success. I've Googled around and only found permanent solutions.
<?php the_post_thumbnail( array(620,295) ); ?>
Any help is appreciated.
Please try the following code.
<?php
$thumbnail_id = get_post_thumbnail_id(get_the_ID());
if (!empty($thumbnail_id))
{
$thumbnail = wp_get_attachment_image_src($thumbnail_id, 'full');
if (count ($thumbnail) >= 3)
{
$thumbnail_url = $thumbnail[0];
$thumbnail_width = $thumbnail[1];
$thumbnail_height = $thumbnail[2];
$thumbnail_w = 620;
$thumbnail_h = floor($thumbnail_height * $thumbnail_w / $thumbnail_width);
}
}
if (!empty ($thumbnail_url)): ?>
<img class="thumbnail" src="<?php echo $thumbnail_url; ?>" alt="<?php the_title_attribute(); ?>"
width="<?php echo $thumbnail_w; ?>" height="<?php echo $thumbnail_h; ?>" />
<?php endif; ?>
http://www.boxoft.net/2011/10/display-the-wordpress-featured-image-without-stretching-it/
What about putting a around that an setting the above code and then using CSS to set the width and height? Something like this:
CSS
.deals img {
width: 620px;
max-height: 295px;
}
HTML / PHP
<div class="deals"><?php the_post_thumbnail( array(620,295) ); ?></div>

Categories