php wordpress get_the_post_thumbnail - array issue (probably easy fix) - php

Basically the format of the image tag I am trying to create is:
<img src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" class="attachment-catalog" data-src="my/image/path/here">
And here is my code:
<?php
if ( has_post_thumbnail( $post->ID ) ) {
$newimgdetails = array(
'src' => "data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=",
'class' => "attachment-$size",
'alt' => trim( strip_tags( $attachment->post_excerpt ) ),
'title' => trim( strip_tags( $attachment->post_title ) ),
'data-src' => $src,
);
echo get_the_post_thumbnail( $post->ID, 'shop_catalog', $newimgdetails);
}
?>
I'm fairly new to php so I'm assuming it's an easy fix, but I've been messing around with this for the past hour.
Can someone help me get this one figured out?
Here is a link to the codex for get_the_post_thumbnail - https://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
*Update - Tried this code out with a picture from google and it works. The issue is my variable $src. I need this to get the path to the image, which it is currently not doing. I used $src because I saw it in the codex link above and thought it would work. lol.

I am asuming that you have registered 'shop_catalog' using add_image_size
<?php
if ( has_post_thumbnail( $post->ID ) ) {
$thumb_id = get_post_thumbnail_id( $post->ID );
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'shop_catalog', true);
$src = $thumb_url_array[0];
echo '<img scr="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" class="attachment-catalog" alt="'.trim( strip_tags( $attachment->post_excerpt ) ).'" title="'.trim( strip_tags( $attachment->post_title ) ).'" data-src="'.$src.'">';
}
?>

Related

WooCommerce: Change image version in FlexSlider gallery navigation

I want to change the image version used by the FlexSlider on the WooCommerce product page.
At the moment the slider uses for its gallery navigation the thumbnail size.
But these image size is cropped and I need an uncropped version.
There is already a size in WooCommerce which I could use: woocommerce_gallery_thumbnail
Unfortunately I couldn't find any hook or option to change the used image.
What I found and use is the filter woocommerce_single_product_carousel_options.
At the moment I'm using only one option of it:
$options['controlNav'] = wp_is_mobile() ? true : 'thumbnails';
But I see no option to change the used image version.
Is there anythin I could use/hook?
OK, I tried to use the following filter:
(https://github.com/woocommerce/woocommerce/blob/df94b6570df6f6002f61c9e53c8054eb77c2c405/includes/wc-template-functions.php#L1499)
function wc_get_gallery_image_html( $attachment_id, $main_image = false ) {
$flexslider = (bool) apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) );
$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
$thumbnail_size = apply_filters( 'woocommerce_gallery_thumbnail_size', array( $gallery_thumbnail['width'], $gallery_thumbnail['height'] ) );
$image_size = apply_filters( 'woocommerce_gallery_image_size', $flexslider || $main_image ? 'woocommerce_single' : $thumbnail_size );
$full_size = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' ) );
$thumbnail_src = wp_get_attachment_image_src( $attachment_id, $thumbnail_size );
$full_src = wp_get_attachment_image_src( $attachment_id, $full_size );
$alt_text = trim( wp_strip_all_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) );
$image = wp_get_attachment_image(
$attachment_id,
$image_size,
false,
apply_filters(
'woocommerce_gallery_image_html_attachment_image_params',
array(
'title' => _wp_specialchars( get_post_field( 'post_title', $attachment_id ), ENT_QUOTES, 'UTF-8', true ),
'data-caption' => _wp_specialchars( get_post_field( 'post_excerpt', $attachment_id ), ENT_QUOTES, 'UTF-8', true ),
'data-src' => esc_url( $full_src[0] ),
'data-large_image' => esc_url( $full_src[0] ),
'data-large_image_width' => esc_attr( $full_src[1] ),
'data-large_image_height' => esc_attr( $full_src[2] ),
'class' => esc_attr( $main_image ? 'wp-post-image' : '' ),
),
$attachment_id,
$image_size,
$main_image
)
);
return '<div data-thumb="' . esc_url( $thumbnail_src[0] ) . '" data-thumb-alt="' . esc_attr( $alt_text ) . '" class="woocommerce-product-gallery__image">' . $image . '</div>';
}
There are options for the $gallery_thumbnail, $thumbnail_size and $thumbnail_src.
I don't know which one ist the correct one to change the output of the gallery navigation thumbnail.
I tried the following code but I'm not sure if that code is correct (it does nothing):
add_filter( 'woocommerce_gallery_thumbnail_size', function( $thumbnail_size ) {
return array(
'woocommerce_gallery_thumbnail',
);
} );
I've read in the docs, that there are only hooks for woocommerce_gallery_thumbnail_size, woocommerce_gallery_image_size and woocommerce_gallery_full_size and they accept name based image sizes, or an array of width/height values.
So I guess the correct one would be woocommerce_gallery_thumbnail_size?!
Or is the whole approach wrong?

Landing Page – Text Area – change text content to excerpt - roseta theme

I'm using Roseta wordpress theme. It contains a Landing page that can feature content from other pages.
I tried to modify the roseta_lptext() function to change the content to the excerpt but it’s still not working.
This is what I changed, I changed ‘content’ to ‘excerpt’ in the apply_filters section :
* text area builder
*/
if ( ! function_exists( ‘roseta_lptext’ ) ):
function roseta_lptext( $what = ‘one’ ) {
$pageid = cryout_get_option( ‘theme_lptext’ . $what );
$pageid = cryout_localize_id( $pageid );
if ( intval( $pageid ) > 0 ) {
$page = get_post( $pageid );
$data = array(
‘title’ => apply_filters( ‘roseta_text_title’, get_the_title( $pageid ), $pageid ),
‘text’ => apply_filters( ‘excerpt’, get_the_excerpt( ‘excerpt’, $pageid ) ),
‘class’ => apply_filters( ‘roseta_text_class’, ”, $pageid ),
‘id’ => $what,
);
list( $data[‘image’], ) = wp_get_attachment_image_src( get_post_thumbnail_id( $pageid ), ‘full’ );
roseta_lptext_output( $data );
Any idea on why it doesn't work ?
I'm new at this so...
Thank you !

How can i display the wordpress taxonomy image into front-end?

I'm trying to display the taxonomy images into front-end, But I don't get how to use the code. Here I'm using a plugin Taxonomy Images [https://wordpress.org/plugins/taxonomy-images/] Please, can someone help me?
Here is my code below:
<?php $terms = get_terms('category');
foreach($terms as $term):
$args = array(
'orderby' => 'title',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($term->slug)
)
));
$tag_query = new WP_Query( $args );
if($tag_query->have_posts()):
$image_url = apply_filters( 'taxonomy-images-queried-term-image-url', '' );
echo '<li>
' . wp_get_attachment_image( $term->image_id, 'detail' ) . '
'.esc_html($term->name).'
<p>'.esc_html($term->description).'</p>
</li>';
while($tag_query->have_posts()):$tag_query->the_post();
endwhile;
endif;
wp_reset_postdata();
endforeach;
?>
If you are using Taxonomy image plugin then use wp_get_attachment_image method to get the image.
Below is complete code for you.
check below code this will help you.
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
print '<ul>';
foreach ( (array) $terms as $term ) {
print '<li>' . wp_get_attachment_image( $term->image_id, 'detail' ) . ' <a href="#">'.esc_html($term->name).'
<p>'.esc_html($term->description).'</p></li>';
}
print '</ul>';
}
Use wp_get_attachment_image() funxtion to show taxnomy images at front-end.
wp_get_attachment_image( $term->image_id, 'detail' )
Note: Pass image_id in wp_get_attachment_image() function.
Reference
Updated Answer:
Yes, You can also get category name and description inside the loop.
$term->name
$term->description

Display randomly picked image from Wordpress's page?

I would like to display one image at a time on my page, that is picked randomly from the images loaded in this page (from wordpress admin).
I am working with my own theme and I have a "front-page.php" file where I have my function to load the content of my page.
I don't know how to add the random image function to this code.
my PHP code:
<?php get_header(); ?>
<div class="container">
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
$images = [];
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$image_url = ( !empty( wp_get_attachment_url( $attachment->ID ) ) ) ? wp_get_attachment_url( $attachment->ID ) : '';
$image_alt = ( !empty( get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true) ) ) ? get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true) : '';
$images[] = array(
'url' => $image_url,
'alt' => $image_alt
);
}
}
$image = $images[array_rand( $images, 1 )];
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php
}
}
?>
</div>
Create an array of available images and then choose one at random.
$images = array(
'http://via.placeholder.com/350x150',
'http://via.placeholder.com/350x150',
'http://via.placeholder.com/350x150',
'http://via.placeholder.com/350x150',
);
$image_url = $images[array_rand( $images, 1 )];
echo $image_url;
If you want to include alt text, use a multidimensional array:
$images = array(
array( 'url' => 'http://via.placeholder.com/350x150', 'alt' => 'My image'),
array( 'url' => 'http://via.placeholder.com/350x150', 'alt' => 'My image'),
array( 'url' => 'http://via.placeholder.com/350x150', 'alt' => 'My image'),
array( 'url' => 'http://via.placeholder.com/350x150', 'alt' => 'My image'),
);
$image = $images[array_rand( $images, 1 )];
echo $image['url'];
echo $image['alt'];
From your response it sounds like you're trying to pull the images from the post content, if so, you'll need to loop through the post's attachments to create the images array. However, there are a number of potential pitfalls with this method. If I were you, I would create a gallery field using Advanced Custom Fields and use that as the source of the image group.
<?php
if ( have_posts() ) {
$images = [];
while ( have_posts() ) {
the_post();
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
// get url from Plugin: WP Gallery Custom Links
$image_href = ( !empty( get_post_meta( $attachment->ID, '_gallery_link_url', true) ) ) ? get_post_meta( $attachment->ID, '_gallery_link_url', true) : '';
// get attachment
$image_url = ( !empty( wp_get_attachment_url( $attachment->ID ) ) ) ? wp_get_attachment_url( $attachment->ID ) : '';
$image_alt = ( !empty( get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true) ) ) ? get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true) : '';
$images[] = array(
'url' => $image_url,
'alt' => $image_alt,
'href' => $image_href // url from WP Gallery Custom Links
);
}
}
}
$image = $images[array_rand( $images, 1 )];
?>
<a href="<?php echo $image['href']; ?>" title="<?php echo $image['alt']; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<?php
}
?>

WordPress theme not pulling through title text of images

I am trying to make the theme I am using (DICE) into pulling through title text of images.
The code I am using is below;
if ( has_post_thumbnail() ) {
if ( $linking == 'lightbox' ) {
$alt = esc_attr(get_the_title( $post->ID ) );
$the_title = esc_attr(get_the_title( $post->ID ) );
$img = get_the_post_thumbnail( $post->ID, $size, array( 'alt' => $alt, 'title' => $the_title ) );
}
else
$img = get_the_post_thumbnail( $post->ID, $size );
}
elseif( $placeholder ) {
$img = btp_render_placeholder( $size, '', false );
}
else {
return;
}
The only line I have added is
$the_title = esc_attr(get_the_title( $post->ID ) );
with
'title' => $the_title
in the array aswell. The alt text is pulling through fine, so I'm unsure as to why the title text isn't pulling through?
Any help is appreciated. Thanks.
Using two separate variables to get the same information is bad programming practice.
Try to use only one variable, like this:
if ( $linking == 'lightbox' ) {
$post_title = esc_attr(get_the_title( $post->ID ) );
//The Image
$img = get_the_post_thumbnail( $post->ID, $size, array( 'alt' => $post_title, 'title' => $post_title ) );
}

Categories