Get image source by ID with size - wordpress

I'm working on a WP site with some promotion / advert sliders with a Google Analytics click event. Works great, now I want to serve the right image on the right resolution.
I'm using picturefill for serving the images. Works fine while hardcoded, so I know it works. When I try to get the image sources with images uploaded by WP is doesn't. I know it due my (lack-off) php skills.
What picturfill needs:
<span data-picture data-alt="">
<span data-src="filename_default.jpg"></span>
<span data-src="filename_small.jpg" data-media="(min-width: 400px)"></span>
<span data-src="filename_medium.jpg" data-media="(min-width: 768px)"></span>
<span data-src="filename_big.jpg" data-media="(min-width: 1200px)"></span>
<!-- Fallback content for non-JS browsers. -->
<noscript>
<img src="external/imgs/small.jpg" alt="">
</noscript>
</span>
I have either the url or the id or the image stored in:
$attachment_id
I thought doing this:
<?php
$attachment_id = get_field('advimg');
$large = "adv-pos-a-large";
$default = "adv-pos-a-default";
$small = "adv-pos-a-small";
?>
The get_field('advimg'); is from ACF.
<span data-picture data-alt="">
<span data-src="<?php wp_get_attachment_image_src( $attachment_id, $default ); ?>"></span>
<span data-src="<?php wp_get_attachment_image_src( $attachment_id, $small ); ?>" data-media="(min-width: 400px)"></span>
<span data-src="<?php wp_get_attachment_image_src( $attachment_id, $default ); ?>" data-media="(min-width: 768px)"></span>
<span data-src="<?php wp_get_attachment_image_src( $attachment_id, $large ); ?>" data-media="(min-width: 1200px)"></span>
<!-- Fallback content for non-JS browsers. -->
<noscript>
<img src="external/imgs/small.jpg" alt="">
</noscript>
</span>
But it ain't working. Iv'e played around with:
wp_get_attachment_image_src
wp_get_attachment_image_url
wp_get_attachment_image_link
I must be having the fridays, due it ain't working and something says to me that it's not that hard... im just not seeing it today.
Hoped you guys could pitch in.
Thanks in advance,
/Paul
EDIT / FINAL CODE / FIX
<?php
$attachment_id = get_field('advanced_custom_field_name_here');
$small = wp_get_attachment_image_src( $attachment_id, 'adv-pos-a-small' );
$default = wp_get_attachment_image_src( $attachment_id, 'adv-pos-a-default' );
$large = wp_get_attachment_image_src( $attachment_id, 'adv-pos-a-large' );
?>
<span data-picture data-alt="alt desc here">
<span data-src="<?php echo $default[0]; ?>"></span>
<span data-src="<?php echo $small[0]; ?>" data-media="(min-width: 400px)"></span>
<span data-src="<?php echo $default[0]; ?>" data-media="(min-width: 768px)"></span>
<span data-src="<?php echo $large[0]; ?>" data-media="(min-width: 1200px)"></span>
<!-- Fallback content for non-JS browsers. Same img src as the initial, unqualified source element. -->
<noscript>
<img src="<?php echo $default[0]; ?>" alt="alt desc here">
</noscript>
</span>

wp_get_attachment_image_src is confusingly named. (A better name for it would be wp_get_attachment_image_atts). It actually returns an array with the image attributes "url", "width" and "height", of an image attachment file. For just the image src, use the first element in the returned array:
$img_atts = wp_get_attachment_image_src( $attachment_id, $default );
$img_src = $img_atts[0];
Also, make sure your ACF image field return type is set to attachment ID so that $attachment_id = get_field('advimg'); gives you the id that you expect.

Related

How to get the URL of the get_avatar_url function in WordPress

Would you be able to help me with my problem?
What I'm trying to do is get the exact URL of the get_avatar_url() function in WordPress. Then use the URL as the background-image url.
Here's how my code looked like.
<?php $avatar_url = get_avatar_url(get_avatar($author->ID, 150), array("size"=>260)); ?>
<div class="col-xs-5 nopadding author-picture" style="background: url('<?php echo $avatar_url; ?>')">
<a class="author-block " href="<?php echo get_author_posts_url($author->ID); ?>">
<figure class="author-picture">
<?php echo get_avatar($author->ID, 150); ?>
</figure>
</a>
</div>
However, on the page it is returning like this:
like this:
<?php $avatar_url = get_avatar_url(get_the_author_meta( 'ID' ), array('size' => 450)); ?>
<div class="col-xs-5 nopadding author-picture" style="background: url('<?php echo esc_url( $avatar_url ); ?>')">
The usage of get_avatar_url is get_avatar_url( mixed $id_or_email, array $args = null ). Why are you passing get_avatar to this function.
You can use this directly like this:
get_avatar_url($author->ID, array("size"=>260 ));
I think the background URL rule must contain a recognizable image extension (jpg, jpeg, gif, png).
To achieve this, you can:
first, get the whole html of the avatar for the specified user ID...
... then extract the url containing the image extension from it, using regex...
and finally use it in your code as follows.
$imageHtml = get_avatar($author->ID, 150);
$imageUrlWithType = [];
preg_match('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $imageHtml, $imageUrlWithType);
<div class="col-xs-5 nopadding author-picture" style="background: url('<?php echo $imageUrlWithType[0]; ?>')">
<a class="author-block " href="<?php echo get_author_posts_url($author->ID); ?>">
<figure class="author-picture">
<?php echo get_avatar($author->ID, 150); ?>
</figure>
</a>
</div>
get_avatar_url( $current_user->ID );

getfield() not working in WordPress

This is the testing code I wrote to display an ad on a site. It does not display the link and image. I have added the link and image using default settings.
<?php
if(1){
echo("<div style='position:absolute; right:142.5px; top:0px; top:303px;'>
<a href='");
echo get_field( 'add_ad_link' );
echo("'><img src=').get_field( 'ad_image' ).('></a></div>");
}
?>
If you are using the correct field names and this is within your loop then you just need to correct your quotes.
<?php if (1) : ?>
<div style='position:absolute; right:142.5px; top:0px; top:303px;'>
<a href="<?php echo get_field( 'add_ad_link' ); ?>">
<img src="<?php echo get_field( 'ad_image' ); ?>">
</a>
</div>
<?php endif; ?>
If you are going to be outputting a large amount of HTML it is sometimes easier to read if you just output the HTML instead of trying to echo a string of html. This makes it easier to keep your quotes in the right order.

Wordpress Arcade theme modification

I'm self learning Wordpress and trying to modify Arcade basic theme.
What I need is to remove the ARCADE title and replace with an image. also I'm trying to get two call to action buttons instead one.
Can someone help me with a solution. I got a fair understanding about PHP however I seems to cant locate any file to think of a modification too.
Thank you
You need to modify your header.php file under the theme folder.
Open wp-content-> themes -> arcade-basic -> header.php
Put the following code under the
<div id="site-meta">
<h1 id="site-title">
<img src="<URL to your LOGO>" /> </h1>
<?php if ( $bavotasan_theme_options[ 'header_icon'] ) { ?>
<i class="fa <?php echo $bavotasan_theme_options['header_icon']; ?>"></i>
<?php } else { $space_class=' class="margin-top"' ; } ?>
<h2 id="site-description" <?php echo $space_class; ?>>
<?php bloginfo( 'description' ); ?>
</h2>
<a href="#" id="more-site" class="btn btn-default btn-lg">
<?php _e( 'Button1', 'arcade' ); ?>
</a>
<a href="#" id="more-site" class="btn btn-default btn-lg">
<?php _e( 'Button2', 'arcade' ); ?>
</a>
</div>
Let us know if you have any queries.
You can Change it Two Way.
1.) You Add Code On Direct Header.php Files.
<div id="site-meta">
<h1 id="site-title">
<a href="<?php echo esc_url( home_url() ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home">
<img src="<URL to your LOGO>" />
</a>
</h1>
This Code Is Dynamic Code It's Come From Admin Panel.And You Also Add Static Site Name Like
<div id="site-meta">
<h1 id="site-title">
<a href="<?php echo esc_url( home_url() ); ?>" title="SITE NAME" rel="home"><img src="<URL to your LOGO>" />
</a>
</h1>
And Second Is.
2). Go Admin Panel -->> Appearance -->> Customize
3). you can create your own header.php file as per your requirement.
Now You See Theme Setting Page Write Your SITE NAME on Site Title & Tagline.

Advanced Custom Fields - Fancybox Slideshow on Clicking a Thumbnail

I'm trying to create a fancybox slideshow that pops up when you click on one thumbnail. I'm using Advanced Custom Fields with Gallery Field.
This is what I have;
<?php
$images = get_field('gallery');
$image_1 = $images[0];
?>
<a href="<?php echo $images['url']; ?>" rel="fancybox">
<img src="<?php echo $image_1['url']; ?>" /></a>
Unfortunately, nothing happens when you click on the imageā€¦
Any leads?
Thanks!
As Pranita said you should be using a for loop for generating the gallery.
If this display more thumbnails than you want, you can simply build your HTML/CSS so that every thumbnail, except the first, is hidden.
Use this, from the official documentation, and customize it to your needs.
<?php $images = get_field('gallery');
if( $images ) : ?>
<div id="carousel" class="flexslider">
<ul class="slides">
<?php foreach( $images as $image ): ?>
<li>
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
The problem is you have used $images['url'] where images is a multidimensional array. It has to be $image_1['url'];
Simply use for loop if you want every image. Else if you want only one image then use
<a href="<?php echo $images[0]['url']; ?>" rel="fancybox">
<img src="<?php echo $images[0]['url']; ?>" /></a>

Wp addthis custom share url

Need to share a custom url which includes the image #ID, so that the url looks the following in addthis:
http://www.mywebsite.com/mygallery/#image-1
Take a look at the following gallery and click on an image:
http://www.starbasket.fr/photos-des-camps/camps-2011/la-baule-3/
Hoe do you include the #image-1 within the share link?
Which WP code echo's out the image ID?
I've tried the following code snippets, but it does not work:
<?php echo $post_thumbnail_id = get_post_thumbnail_id( $post_id ); ?>
<?php echo get_post_thumbnail_id() ?>
<?php echo wp_get_attachment_metadata( $attachment_id, $unfiltered ); ?>
Regards,
Charl
You have to edit gallery's files.
Look in /wp-content/plugins/nextgen-gallery/view/
You'll find several files, the one you want to edit is "gallery.php".
Replace the title which usually diplays the alternate text ($image->alttext).
As the gallery take the "title" of each thumbnail to diplay it bigger, you can put your html code here (you also have to encode it) :
Try mine (the one you want) :
<?php foreach ( $images as $image ) : ?>
<div id="ngg-image-<?php echo $image->pid ?>" class="ngg-gallery-thumbnail-box" <?php echo $image->style ?> >
<div class="ngg-gallery-thumbnail" >
<?php $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."%23".$image->pid;
$url2 = $image->imageURL;
$url3 = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."#".$image->pid; ?>
<a href="<?php echo $image->imageURL ?>" id="<?php echo $image->pid ?>" title="<?php //echo $image->alttext ?><?php
echo '<div class="nggfb" style="float:left;width:80px" class="share-custom" ><a class="share-custom sd-button" href="javascript:postToFeed(\''.$url3.'\',\''.$url2.'\');" style="float:left"><span style="background-image:url(&quot;http://www.starbasket.fr/wp-content/plugins/jetpack/modules/sharedaddy/images/facebook.png&quot;);">Partager</span></a></div>
<div class="tweets" style="width:100px;float:left">
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://platform.twitter.com/widgets/tweet_button.html?url='.$url.'&counturl='.$url.'&hashtags=StarBasket&related=campstarbasket&via=campstarbasket&text=Une photo ! '.$url2.'" style="width:130px; height:20px;">Tweet</iframe>
</div>
<div style="float:left;width:80px;">
<iframe src="/wp-content/googleplus.php?url='.$url.'" marginheight="0" marginwidth="0" frameborder="0" scrolling="no" style="border:0;width:100px;height:20px;"></iframe>
<meta itemprop="image" content="'.$url2.'">
</div>
<a class="small-button smallblack " style="height: 18px;padding-top: 0;" onClick=" return copy(\''.$url3.'\')" href="'.$url3.'">Copier</a>' ?>" <?php echo $image->thumbcode ?> >
<?php if ( !$image->hidden ) { ?>
<img title="<?php echo $image->alttext ?>" alt="<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
<?php } ?>
</a>
</div>
</div>
<?php if ( $image->hidden ) continue; ?>
<?php if ( $gallery->columns > 0 && ++$i % $gallery->columns == 0 ) { ?>
<br style="clear: both" />
<?php } ?>
<?php endforeach; ?>
I made different url because of facebook and the copie/paste function ("#" or is html code "%23") et I also get the file url in order to display on some twitter client without access the web page. And for Facebook, you can use the share function, because it takes the meta og:url of you whole page (without "#imageID"), so I choose the dialog function to do so.
Google+ takes the good one but not the thumbnail.

Resources