Reputation: 37
I'm trying to make a dynamic bootstrap carousel, where I want to display the blog's thumbnail and the title.
I'm using bootstrap 4 to fulfill my needs.
when I implied the code, it got me this error on my localhost
Fatal error: Uncaught Error: Call to undefined function wp_get_attachment_src() in C:\wamp64\www\wpress\wp-content\themes\bassfacemusics-in\index.php on line 35
here's the full line of code from where the carousel starts:
<div id="slider" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php
$args = array(
'cat' => 1,
'post_per_page' => 4
);
$query = new WP_Query( $args );
?>
<?php if($query->have_posts()) : ?>
<?php $i = 0; ?>
<?php while($query->have_posts()) : $query->the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_src($thumb_id,true); ?>
<div class="carousel-item <?php if($i === 0): ?>active<?php endif; ?>">
<img src="<?php $thumb_url[0] ?>" alt="<?php the_title();?>">
</div>
<?php $i++; ?>
<?php endwhile ?>
<?php endif ?>
<?php wp_reset_postdata(); ?>
</div>
<!-- Controls -->
<a href="#slider" class="carousel-control-prev" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a href="#slider" class="carousel-control-next" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
Upvotes: 0
Views: 2080
Reputation: 2505
There is not function called wp_get_attachment_src(int $id, bool $icon)
in Wordpress.
It seems like you want to use wp_get_attachment_image_src()
instead.
To use it you need to change the following line:
$thumb_url = wp_get_attachment_src($thumb_id,true);
To this:
$thumb_url = wp_get_attachment_image_src($thumb_id, 'thumbnail', true);
The second paramter has the following doducmentation:
(string|array) (Optional) Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order).
Default value: 'thumbnail'
Because you seem to want to set the third parameter, you can not simply skip the second parameter, but need to set it explicitly. That is why 'thumbnail'
is provided as a paremter in the call and not omitted, eventhough it is the default value.
The result will be an array with the information about an image, or false
in case of an error.
array [
0 => url
1 => width
2 => height
4 => is_intermediate
]
An example implementation is:
$image = wp_get_attachment_image_src($attachment_id);
if ($image !== false) : ?>
<img src="<?= $image[0] ?>" width="<?= $image[1] ?>" height="<?= $image[2] ?>" />
<?php endif ?>
Upvotes: 0