Problem with getting count with Wordpress - php

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>

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

How to pass a parameter in a variable

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"
>

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

Kirby CMS - add an image modified timestamp

I'm learning Kirby CMS.
I have a page displaying a series of images, I'm unsure how to get the date the image was uploaded.
I think it is something to do with the $file variable?:
$file->modified($format=false)
the last modified timestamp
here is my php loop:
<?php if($page->hasImages()): ?>
<ul class="gallery">
<?php foreach($page->images() as $image): ?>
<div class="gallerySegment">
<h3 class="guidelineHead"><?php echo $image->title() ?></h3>
<p><?php echo $image->caption() ?></p>
<li><img src="<?php echo $image->url() ?>" width="<?php echo $image->width() ?>" height="<?php echo $image->height() ?>" alt="<?php echo $image->title() ?>" /></li>
</div>
<?php endforeach ?>
</ul>
<?php endif ?>
I think this isn't possible by default in kirby 1. You could use the php-function filetime().
In case of Kirby 2 you can write $file->modified($format = false) as in your proposal.
You should be able to simply do something like this within your loop.
<p>Uploaded at: <?php echo $image->modified('d. F Y'); ?></p>
This is available in Kirby 1 as well as in Kirby 2 as a single "image object" you have in your $image variable is always an extended "file object". That means you can use all file object methods on image objects as well.
If you need any information about how set the $format ('d. F Y'), you should have a look at PHPs date() function explanation

PHP of custom field inside PHP Tag

I´m display a slider that get´s the images from custom field, now I need that the slider shows different images depending on the language (es - ca):
This is the code of the slider:
<a href="<?php the_field('slider_home_1_enlace') ?>">
<img src="<?php the_field('slider_home_1'); ?>">
</a>
So I´m creating a conditional tag to load images depending of the language of qtranslate plugin:
<?php
_e('<!--:es-->
<a href="<?php the_field('slider_home_1_enlace') ?>">
<img src="<?php <the_field('slider_home_1'); ?>">
</a>
<!--:-->
<!--:ca-->
<a href="<?php the_field('slider_home_1_enlace_ca') ?>">
<img src="<?php the_field('slider_home_1_ca'); ?>">
</a>
<!--:-->');
?>
I´m a php begginer so I see the problem maybe that the php is inside another php, because this way its not working, if i just put text between tags it works properly for the language.
Any ideas how to syntax this?
You need to check language first, using if - else
detect the language and store it to the variable named $language and then check the condition as below.
<?php
if(qtrans_getLanguage()=="es"){
?>
<a href="<?php the_field('slider_home_1_enlace'); ?>">
<img src="<?php the_field('slider_home_1'); ?>">
</a>
<?php
} else if(qtrans_getLanguage()=="ca"){
?>
<a href="<?php the_field('slider_home_1_enlace_ca'); ?>">
<img src="<?php the_field('slider_home_1_ca'); ?>">
</a>
<?php
}
?>
You can't have php-tags inside each other.
Use basic string concatenation to combine your strings: http://php.net/manual/en/language.operators.string.php
You can't use it that way. Compare: (I guess that the_field() returns the string ;))
<a href="'.the_field("slider_home_1_enlace").'">
<img src="'.the_field("slider_home_1").'" />
</a>
There are multiple mistakes. At first, you can't use the
'.(your things here).'
Second, theres a < in your fourth line and you forget to close your img-tag.
First, install the qTranslate Wordpress plugin.
Then, you can try this:
<?php
$slider_img_fields_langs = array(
'',
'ca',
'es',
);
$lang = qtrans_getLanguage();
if( !in_array( $lang, $slider_img_fields_langs ) );
$lang = '';
echo '
<a href="' . the_field( 'slider_home_1_enlace' . $lang ) . '">
<img src="' . the_field( 'slider_home_1' . $lang ) . '">
</a>';
?>

Categories