Retrieve img from json php - php

I have a loop with different json. Inside the json, there are images some of which have a description and some do not. I want when the description is "Main image" then to bring specific url. But when the description is different from "Main image", then bring another image which I have set.
Τhe problem is that it shows me both images, because in some json there is a blank description but also this description is "Main image"
My code is:
$no_image = get_stylesheet_directory_uri().'/img/no_image.png';
$images = $json->images;
foreach ($images as $img){
if($img->description == 'Main image'){
$main_image = $img->url;
?>
<div class="card__image" style="background-image: url(<?php echo $main_image;?>)"></div>
<?php
}else if($img->description==""){
<div class="card__image" style="background-image: url(<?php echo $no_image;?>)"</div>
}
}

There is no need to have else if (another condition) if you just want to display no_image if description is not "Main Image", just use else
Hence, Change
if($img->description == 'Main image'){
$main_image = $img->url;
?>
<div class="card__image" style="background-image: url(<?php echo $main_image;?>)"></div>
<?php
}else if($img->description==""){
<div class="card__image" style="background-image: url(<?php echo $no_image;?>)"</div>
}
to
<?php
if($img->description == 'Main image'){
$main_image = $img->url;
?>
<div class="card__image" style="background-image: url(<?php echo $main_image;?>)"></div>
<?php }else{ ?>
<div class="card__image" style="background-image: url(<?php echo $no_image;?>)"</div>
<?php } ?>

This bit needs two ==. one = assigns not checks if equal.
}else if($img->description==""){

Related

Get post author image - Wordpress

In WordPress I need to fetch the Author image of author who created post. I tried to get the profile image in this way, but it didn't work.
Here is the code I have written to get other author_meta_details.
<div class="row">
<div class="avatar col-md-3">
<picture class="avatar-circle">
<?php if($avatar = get_avatar(get_the_author_meta('ID')) !== FALSE): ?>
<img src="<?php echo $avatar; ?>" alt="">
<?php else: ?>
<img src="/wp-content/themes/lymited-child/images/avatar-image.png">
<?php endif; ?>
</picture>
</div>
<div class="author-details col-md-9">
<div class="author-name"><strong><?php echo get_the_author_meta('first_name'); ?></strong> - <?php echo get_the_author_meta('nickname'); ?>
</div>
<div class="author-summery"><?php echo get_the_author_meta('description'); ?>
</div>
</div>
</div>
Try this
<?php
$get_author_id = get_the_author_meta('ID');
$get_author_gravatar = get_avatar_url($get_author_id, array('size' => 450));
if(has_post_thumbnail()){
the_post_thumbnail();
} else {
echo '<img src="'.$get_author_gravatar.'" alt="'.get_the_title().'" />';
}
?>
OR Remove this if the condition and try
echo get_avatar( get_the_author_meta('ID') );
get_avatar() returns <img> element so you don't need to wrap it to img again
Also you need to wrap $avatar = ... to parenthesis as = priority is lower than !==
try to replace this
<?php if ( $avatar = get_avatar(get_the_author_meta('ID')) !== FALSE): ?>
<img src="<?php echo $avatar; ?>" alt="">
with this
<?php if ( ( $avatar = get_avatar(get_the_author_meta('ID')) ) !== FALSE): ?>
<?php echo $avatar; ?>

How can I put this php into a image background url?

I would be very very grateful for help with this:
I have a bootstrap carousel that I want to include pictures from a database. I want it to appear as background image like this:
<div class="carousel-item" style="background-image: url('link to image')">
This is the PHP-snippet that I want to put in place for the above 'link to image':
<?php
$img_url = "images/programpics/"; {
echo '<img src="' . $img_url . $row_firstrow[ 'show_tix_phone' ] . '"id="pic1" alt="Something here" />';
}
?>
'Link to image' is a part of style, not allow html tags like 'img' and angle brackets. Example syntax:
background-image: url('images/bg.jpg')
With this in mind you code must be like this:
<div class="carousel-item" style="background-image: url('<?php
$img_url = "images/programpics/";
{
echo $img_url . $row_firstrow[ 'show_tix_phone' ];
}
?>')" id="pic1" alt="Something here" />';
Hope help:
<div class="carousel-item" style="background-image: url(<?php echo $img_url . $row_firstrow[ 'show_tix_phone' ] ?>)">

Display default image as div background, when there is no post thumbnail in WordPress

I have the following expression, to get the post thumbnail, and if the post has no thumbnail, it should set a default image as the <div> background.
<?php $backgroundImg = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');if(empty($backgroundImg)) $backgroundImg = APP_URL . "images/common/no-image.png";?>
<div class="ach_img" style="background: url('<?php echo $backgroundImg[0]; ?>') no-repeat;"></div>
The problem is that it's returning style="background: url('h') no-repeat;. I guess the problem is in <?php echo $backgroundImg[0]; ?>, but I can't figure out how to correct it.
$backgroundImg[0] will return only the first character of APP_URL if no featured image. Try this instead.
<?php
$backgroundImg = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); $bgimg=$backgroundImg[0];
if(empty($backgroundImg)) $bgimg = APP_URL . "images/common/no-image.png";
?>
<div class="ach_img" style="background: url('<?php echo $bgimg; ?>') no-repeat;"></div>
Based on the following information of the official code reference:
returns an array (url, width, height, is_intermediate), or false, if no image is available.
source: https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/
You can try the following solution:
<?php
$att_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
if ($att_image === false) {
$imgPath = APP_URL.'images/common/no-image.png';
} else {
$imgPath = $att_image[0];
}
?>
<div class="ach_img" style="background: url('<?= $imgPath ?>') no-repeat;"></div>
$post_id = get_the_ID(); // Get current page ID
$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($post_id), 'full' ); // To get source path of featured image.
$image_url = $feat_image[0]; // This will return thumbnail image path
Now check admin user has selected featured image or not. If it is empty, it means not selected.
if(empty($image)){
$bgimg_path = "Your Image Path";
echo '<div class="bgimage-section" style="background: url('<?php echo $bgimg_path; ?>') no-repeat;"></div>';
}
Hope this code will help you.
You can use var_dump($backgroundImg) to see the result returned, and edit from there.

Dynamically change background image in the CSS during WordPress loop (media queries)

I currently have a carousel on my page inside a Wordpress loop. So as it loops through it changes the background image according to which post I am currently on in the loop. However, I want to use media queries to load smaller images if for instance I the screen size is smaller.
This is what I currently have:
while( $loop->have_posts() ) : $loop->the_post();
$class = '';
// Custom Posts
$carousel_image_1 = get_field('carousel_image_1');
$image_1_title = get_field('image_1_title');
if( $slide_counter == 0 ) { $class .= ' active'; }
?>
<div class="item <?php echo $class ?> <?php echo "slide_" . $slide_counter ?>">
<!-- Set the first background image using inline CSS below. -->
<a href="<?php echo get_post_permalink(); ?>" target="_blank">
<div class="fill" style="background-image:url(<?php echo $carousel_image_1['url']; ?>); background-size:cover;" alt="<?php echo $carousel_image_1['alt']; ?>"> </div>
<div class="carousel-caption">
<h2><?php echo $image_1_title; ?></h2>
<img width="80" class="book" src="<?php bloginfo('stylesheet_directory'); ?>/assets/img/book.png" alt="">
</div>
</a>
</div>
<?php $slide_counter++; ?>
<?php endwhile; wp_reset_query(); ?>
You can't use media queries inline, and you won't be able to have the dynamic flexibility of your variables in CSS, because PHP is server-side.
But, you could do this with JavaScript as long as the JS is able to get the PHP variables (my example uses jQuery):
<?php
while( $loop->have_posts() ) : $loop->the_post();
$class = '';
// Custom Posts
$carousel_image_1 = get_field('carousel_image_1');
$carousel_image_2 = get_field('carousel_image_2'); // added this, for different size image
$image_1_title = get_field('image_1_title');
if( $slide_counter == 0 ) { $class .= ' active'; }
?>
<div class="item <?php echo $class ?> <?php echo "slide_" . $slide_counter ?>">
<a href="<?php echo get_post_permalink(); ?>" target="_blank">
<div class="fill" style="background-size:cover;" alt="<?php echo $carousel_image_1['alt']; ?>"> </div>
<div class="carousel-caption">
<h2><?php echo $image_1_title; ?></h2>
<img width="80" class="book" src="<?php bloginfo('stylesheet_directory'); ?>/assets/img/book.png" alt="">
</div>
</a>
</div>
<script>
var resizeTimer;
function resizer() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
var windowWidth = $(window).width();
if (windowWidth >= 992) { // for width 992 pixels and up
$('.fill').css({
'background-image': 'url(<?php echo $carousel_image_1["url"]; ?>)';
});
} else { // smaller sizes
$('.fill').css({
'background-image': 'url(<?php echo $carousel_image_2["url"]; ?>)';
});
}
}, 200);
}
resizer();
$(window).on('resize', resizer);
</script>
<?php $slide_counter++; ?>
<?php endwhile; wp_reset_query(); ?>
I haven't tested it but I think it should work. You can also adjust the timeout to your preference (right now it's 200ms).
Or if you wanted to not make it truly responsive, and just set the background on page load, you could just write:
<script>
if (windowWidth >= 992) { // for medium size width and up
$('.fill').css({
'background-image': 'url(<?php echo $carousel_image_1["url"]; ?>)';
});
} else { // smaller sizes
$('.fill').css({
'background-image': 'url(<?php echo $carousel_image_2["url"]; ?>)';
});
}
</script>
The following Html CSS will show image in mobile view only
Html:
foreach($data as $item):
echo "
<div class='col-lg-4'>
<div class='panel panel-primary backImg' style='background-image:url({$item['imageLink']}); background-size:100% 100%;'>
Some Text
</div>
</div>";
endforeach;
Css:
#media(min-width: 480px){
.backImg{
background-image:none !important;
}
}

PHP + Wordpress : foreach repeat html structure after every nth

I am trying to achieve result with below structure in foreach loop where after every two images it will repeat entire structure.
I have some basic knowledge for something I can use eg. counter++; and than %2 but don't know syntax and how to use it for my code.
<?php
function dt_attached($postid=0, $size='thumbnail', $attributes='', $linksize='full', $count=-1) {
if ($postid<1) $postid = get_the_ID();
if ($images = get_children(array(
'post_parent' => $postid,
'post_type' => 'attachment',
'numberposts' => $count,
'post_mime_type' => 'image',)))
foreach($images as $image) {
$attachment=wp_get_attachment_image_src($image->ID, 'thumbnail');
$small_image = wp_get_attachment_image_src($image->ID, 'midium');
$big_image = wp_get_attachment_image_src($image->ID, 'full');
?>
<div class="mainrow">
<div class="block">
<a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
<img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
</a>
</div>
<!--[I want to get two images in mainrow]-->
<div class="block">
<a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
<img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
</a>
</div>
</div>
<?php //the_attachment_link($image->ID, false, true, false); ?>
<?php }
}
?>
So what I want is if there is more than two images it will repeat entire html structure. Thanks a lot for your help
What I gathered from your comments is that you want to display two images per row, and if there's one extra image, to display a placeholder next to it in the final row.
All you need is a count of how many images there are, and whether it's an even or odd number. Then once you know you're at the last image (using an incrementing counter), you add the placeholder:
What your code doesn't do is place two images in one row. For that we need to take the modulo (%) of the counter as well.
<?php
$counter = 0;
$imgCount = count($images);
foreach ($images as $image) {
$attachment = wp_get_attachment_image_src($image->ID, 'thumbnail');
$small_image = wp_get_attachment_image_src($image->ID, 'midium');
$big_image = wp_get_attachment_image_src($image->ID, 'full');
if ($counter % 2 == 0): ?>
<div class="mainrow">
<?php endif; ?>
<div class="block">
<a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
<img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
</a>
</div>
<?php if ($counter++ % 2 == 1): ?>
</div>
<?php endif; ?>
<?php //the_attachment_link($image->ID, false, true, false); ?>
<?php
}
// Since (if there are an odd number of images) the loop may not close the <div>,
// we have to make sure it does.
if ($counter % 2 == 0) {
?>
<!-- placeholder goes here -->
</div>
<?php
}

Categories