Using scrset & Fancybox with ACF in Wordpress - wordpress

I'm trying to get behind this for a while now but it does not seem to work whatever I try.
Basically I have a Fancybox, that is working and I have placed an img with ACF with scrset value but I can't combine both because Fancybox is looking for the pure img value in Wordpress, while I need the img-id for the scrset value.
This is what I got so far working for me.
Fancybox:
<div class="img01">
<a data-fancybox="Gallery" class ="lightbox" href="img01.jpg" data-caption="text">
<?php $image = get_field('img01');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>"
alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</a></div>`
(function($){$(document).ready(function(){
$('.lightbox').fancybox();});})(jQuery)
ACF Img with scrset value
<div class="img01">
<a data-fancybox="Gallery"
class ="lightbox" href="img01"
data-fancybox
data-caption="<?php echo $img_alt_text ?>">
<?php $img = get_field('image_01');
$img_src = wp_get_attachment_image_src( $img, '80vw' );
$img_srcset = wp_get_attachment_image_srcset( $img, '80vw' );
$img_alt_text = get_post_meta( $img, '_wp_attachment_image_alt', true);
?>
<?php if( $img_src ){ ?>
<img class="img01"
src="<?php echo esc_url( $img_src[0] ); ?>"
title="<?php the_title(); ?> - <?php echo $job ?>"
srcset="<?php echo esc_attr( $img_srcset ); ?>"
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, (min-width: 1200px) 50vw, 1600px"
alt="<?php echo $img_alt_text ?>"
/>
I would love to combine those so i can add an image to acf that behaves responsive but also opens in fancybox on click. But there is probably also an easier way to do this i suppose.
Honestly have no idea what im doing with that and hoping for help. Im happy to provide more input. Thank you.
New approach:
This function should in theory do the job but I still cant figure it out
// retrieves the attachment ID from the file URL
function pippin_get_image_id($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $attachment[0];
}
What am I missing/doing wrong?
<a data-fancybox="Gallery"
class ="lightbox"
href="<?php $image = get_field('img01');
echo $image['url'];?>"
data-caption="<?php echo $image['alt']; ?>">
<?php
$image = get_field('img01');
$image_url = get_field('img01');
$image_id = pippin_get_image_id($image_url);
$img_src = wp_get_attachment_image_src( $image_id, '80vw' );
$img_srcset = wp_get_attachment_image_srcset( $image_id, array( 100, 100 ) );
if( !empty($image) ): ?>
<img class=""
src="<?php echo esc_url( $img_src[0] ); ?>"
srcset="<?php echo esc_attr( $img_srcset ); ?>"
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, (min-width: 1200px) 50vw, 1600px"
alt="<?php echo $img_alt_text ?>"
/>
<?php endif; ?>
</a>

Related

Wordpress Custom Logo

I'm pretty new about coding, and i'm trying to build a Wordpress custom theme.
I'm stacked on the logo section and I really can't understand what i'm doing wrong:
<?php if ( get_theme_mod('apc_logo_image', '') != '' ): ?>
<img class="animate" src="<?php echo get_template_directory_uri(); ?>/img/logo.png" alt="logo sito">
<?php elseif ( is_front_page() && is_home() ) : ?>
<img class="animate" src="<?php echo get_theme_mod( 'apc_logo_image', '' ); ?>" alt="<?php echo get_theme_mod( 'apc_logo_alt_text', '' ); ?>">
<?php else : ?>
<h1 class="site-title"><?php bloginfo( 'name' ); ?></h1>
<?php endif; ?>
I would like to do the follow:
If I upload an images from Customizer, than it would be visible as logo.
If don't and i have a logo in a specific path like: /assets/logo ecc... than it should be visible like logo.
if don't, then I would like to see the name of the website as it should be.
So working code:
<?php
$logoFile = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/' . wp_get_theme()->get('TextDomain') . '/assets/images/logo/logo.png';
if ( (bool)get_theme_mod('apc_logo_image')) : ?>
<img class="animate" src="<?php echo get_theme_mod( 'apc_logo_image'); ?>" alt="<?php echo get_theme_mod( 'apc_logo_alt_text'); ?>">
<?php else : ?>
<?php if ( file_exists($logoFile) ) : ?>
<img class="animate" src="<?php echo '/wp-content/themes/' . wp_get_theme()->get('TextDomain') . '/assets/images/logo/logo.png'?>" alt="logo sito">
<?php else : ?>
<h1 class="site-title"><?php bloginfo( 'name' ); ?></h1>
<?php endif; ?>
<?php endif ?>

get_the_post_thumbnail_url fullsize dIt does not work

The following function should call the full-size image, but it doesn't.
<?php echo get_the_post_thumbnail_url($next_id, 'fullsize'); ?>
How could it be forced?
It does not work in amp but it does in no amp.
Complete code that I am using.
<?php if(has_post_thumbnail()) {?> <?php if(is_amp_endpoint()) { ?>
<amp-img class="amp-wp-enforced-sizes" src="<?php echo get_the_post_thumbnail_url($nextID, 'full'); ?>" width="372" height="186" alt="<?php echo esc_attr($post->post_title); ?>" layout="responsive" >
<noscript><img decoding="async" alt="AMP" src="<?php echo get_the_post_thumbnail_url($next_id, 'full'); ?>"></noscript>
</amp-img>
<?php } else {?>
<?php echo get_the_post_thumbnail($next->ID, 'fullsize'); ?>
<?php }} ?>
Thank
They keyword here is 'full' not 'fullsize'
Correct code is
<?php echo get_the_post_thumbnail_url($next_id, 'full'); ?>
Reference
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/#source

How to dynamic image URL in a WordPress plugin?

How to dynamic image URL in a WordPress plugin?
This is how you get the featured image's URL:
<?php if (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />
<?php } ?>

ACF repeater field custom image size output

I'm trying to output custom image size in my code:
<?php if(get_field('repeater-field-name')) { ?>
<?php foreach (get_field('repeater-field-name') as $row) :?>
<div class="item" onclick="location.href='<?php print $row['link'] ?>';" style="cursor: pointer;">
<div class="sisseviskaja-title"><h2><?php print $row['title'] ?></h2></div>
<div class="sisseviskaja-pilt"><img src="<?php print $row['image'] ?>" alt="" /></div>
</div>
<?php endforeach; ?>
<?php } ?>
I have tried ACF tutorial codes but i'd like to keep foreach loop that i have. I'm no PHP guru so i havent got any good ideas to implement it.
Can someone, please, help me?
Here's the way to do with ACF codes. This is using array for the image (which you need to do if you want to pull out the dimensions). I'm not sure exactly if it can be done the way you want to do it.
<?php if (have_rows('project_listings')){ ?>
<?php while (have_rows('project_listings')){ the_row();
$link = get_sub_field('link_field');
$title = get_sub_field('title_field');
$image = get_sub_field('image');
//additional vars
$size = 'full';
$width = $image['sizes'][ $size . '-width' ];
$height = $image['sizes'][ $size . '-height' ];
?>
<div class="item" onclick="location.href='<?php echo $link ?>';" style="cursor: pointer;">
<div class="sisseviskaja-title">
<h2><?php echo $title ?></h2>
</div>
<div class="sisseviskaja-pilt">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>"/>
</div>
</div>
<?php }; ?>
<?php }; ?>
I do it like this:
<?php if( have_rows('field-name') ): ?>
<?php while( have_rows('field-name') ): the_row(); ?>
<?php
$image = get_sub_field('picture');
$size = 'thumbnail'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size );
}
?>
<?php endwhile; ?>
<?php endif; ?>

simple Wordpress If/Else

I have two different methods for displaying Wordpress Thumbnails
What I would like to do is, display the first method, and if it is not available then to display thumbs using the second method.
Below are the Two Methods for displaying Post Thumbnails.
Method 1
<!--Begin WordPress Featured post thumbnail-->
<div class="postthumbcon">
<?php
// check if the post has a featured image assigned to it.
if ( has_post_thumbnail() ) {
// get the src of the large size featured image
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
$thumbnailSrc = $src[0];
// output image resized with timthumb
?>
<div class="postthumb">
">
<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $thumbnailSrc; ?>&h=125&w=165" alt="">
</div>
<?php } ?>
</div>
<!--end WordPress Featured post thumbnail-->
Here is the second method.
<!--Begin Timthumb thumbnail-->
<?php // This will show the image and link the image to the post. Alter the width and height (in both places) to your needs. ?>
<?php if ( get_post_meta($post->ID, 'thumb', true) ) { ?>
<div class="postthumb">
" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=150&w=150&zc=1" alt="<?php the_title(); ?>" width="150" height="150" />
</div>
<?php } ?>
<!--End Timthumb thumbnail-->
Try something like this:
<div class="postthumbcon">
<?php
if ( has_post_thumbnail() ) {
// method one
} else if ( get_post_meta($post->ID, 'thumb', true) ) {
// Method two
}
?>
</div>
This will see if the post has a thumbnail, which you were already doing, but if it doesn't exist it will run the else clause meaning the second if statement will run only if the first fails.
Also, you appear to have the first part of what I am guessing to be a link in method two, see this line:
" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=150&w=150&zc=1" alt="<?php the_title(); ?>" width="150" height="150" />
Here's how your code could look in full:
<div class="postthumbcon">
<?php
if ( has_post_thumbnail() ) {
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
$thumbnailSrc = $src[0];
?>
<div class="postthumb">
<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $thumbnailSrc; ?>&h=125&w=165" alt="" />
</div>
<?php } else if ( get_post_meta($post->ID, 'thumb', true) ) { ?>
<div class="postthumb">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=150&w=150&zc=1" alt="<?php the_title(); ?>" width="150" height="150" />
</a>
</div>
<?php } ?>
</div>

Resources