How to define current product thumbnail in woocommerce? - php

I am currently working on a shop page which works with Wordpress + Woocommerce Plugin. I edited the woocommerce Files so that I have custom products, menus and so on.
I managed to get current images from my products, but now I want them to appear on click (the little image Icon, on the Screenshot)
But instead of showing the current product image it shows the first image of each category
.
Here is my current code that loads the wrong product thumbnail:
<div class="modal-content">
<a itemprop="image" href="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" class="zoom" rel="thumbnails" title="<?php echo get_the_title( get_post_thumbnail_id() ); ?>">
<?php echo get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) ) ?>
</a></div>
It is located in woocommerce/content-product.php.
Thank you in advance
Nico

So this is the solution, I had to create dynamic IDs. As I wrote it was always the same image, the reason of this was that there was only one ID and not multiple/dynamic.
This was the code I had to add to the id:
<div id="modal-<?php echo $product->id ?>" class="modal">
And here is the other one:
<a data-target="modal-<?php echo $product->id; ?>" class="modal-trigger"><i class="material-icons red-text right">photo</i></a>

Related

Replace Button in woocommerce by a clickable Image in PhP

I sell online Gift Cards using Woocommerce on Wordpress. On the thank you page it displays the download image (containing the Gift Card Key) as a button.
I would like it to display the image itself to be downloaded in full size above the the download link.
Screenshot of the Thank You Page
Here's the part that displays the download section in PHP:
<?php break;
case 'download-file' : ?>
<a href="<?php echo esc_url( $download['download_url'] ); ?>" class="woocommerce-MyAccount-downloads-file">
<?php echo esc_html( $download['file']['name'] ); ?>
</a>
Can you help me out plz?
Try this code. you have to download attribute to <a> tag
<?php
break;
case 'download-file' : ?>
<a href="<?php echo esc_url( $download['download_url'] ); ?>"
class="woocommerce-MyAccount-downloads-file" download>
<?php echo esc_html( $download['file']['name'] ); ?>
</a>

Random thumbnails Wordpress

I'm trying to load 4 random thumbnails at the end of each post on Wordpress and each thumbnail should link to its respective post. I can't figure out how to do it properly, since I have very little knowledge of PHP. I have found this code snippet in the theme that get the thumbnail and the title:
<div class="col-sm-4 col-md-4 col-lg-4 tiles" id="post-<?php the_ID(); ?>" <?php post_class( 'post' ); ?> itemscope="" itemtype="http://schema.org/BlogPosting">
<?php
if ( has_post_thumbnail() ) {
echo '<div class="post-thumbnail">';
echo '<a href="' . esc_url( get_permalink() ) . '">';
echo get_the_post_thumbnail( $post->ID, 'shop_isle_blog_image_size' );
echo '</a>';
echo '</div>';
}
?>
<div class="tile_title">
<a class="post-title"><?php the_title(); ?></a>
</div>
</div>
Furthermore, the thumbnails should come from the same category as the current post. E.g. if someone is reading a blogpost from the "Travel" category the 4 thumbnails should only come from this category, and the same goes for the rest of the categories. Is it possible to do this?
This is called "related posts" feature. There are many plugins for this, for example YARPP is one such plugin.
If you want to avoid plugin, you can get theme that has this functionallity in the template itself. My blog for example uses Ribbon theme and contains this code.

Woocommerce fancybox gallery on products

I'm using fancybox plugin as a lightbox plugin. On my woocommerce product page I have created a link that should open a gallery associated with each product (the code below is in content-product.php inside the loop so it works fine).
global $product;
$attachment_ids = $product->get_gallery_attachment_ids();
<div class="product_quick_view">
<?php $first_image = wp_get_attachment_image_src($attachment_ids[0], 'full'); ?>
<a class="fancybox" rel="group<?php echo $product->id; ?>" href="<?php echo $first_image[0]; ?>"><?php echo esc_html('Quick view', 'theme'); ?></a>
<div class="hidden_gallery">
<?php foreach($attachment_ids as $image_id){
$image_url = wp_get_attachment_image_src( $image_id, 'full' );
echo '<a class="fancybox" rel="group'.$product->id.'" href="'.$image_url[0].'">'.wp_get_attachment_image( $image_id, 'full' ).'</a>';
}; ?>
</div>
</div>
Which outputs this
<div class="product_quick_view">
<a class="fancybox" rel="group3709" href="../uploads/2014/09/image1.jpg">Quick view</a>
<div class="hidden_gallery">
<a class="fancybox" rel="group3709" href="../uploads/2014/09/image1.jpg">
<img width="552" height="820" src="../uploads/2014/09/image1.jpg" class="attachment-full" alt="image1">
</a>
<a class="fancybox" rel="group3709" href="../uploads/2014/09/image2.jpg">
<img width="552" height="820" src="../uploads/2014/09/image2.jpg" class="attachment-full" alt="image2">
</a>
</div>
</div>
The .hidden_gallery class will make the images in that div not visible. This should make it so that the fancybox will go through all the images in the same rel attribute.
But for some reason, it's not doing that. When I click the 'Quick view' link, I get the image that is attached to that link, but no other images (and they are in the DOM).
Is this because the fancybox jQuery fired before the images could be loaded with the woocommerce? I have it inside the $(document).ready() environment, should I wrap it inside the .load() environment or use the .imagesLoaded plugin?
Ok, found the answer. Apparently fancybox is not working, and is generally having issues with woocommerce, so I used the one that comes with woocommerce.
Just add this
<a itemprop="image" data-rel="prettyPhoto[product-gallery]<?php echo $product->id;?>" href="...
This will group them and you'll use woocommerce's own fancybox that works.

How to add Title Attribute to Wordpress Image on Attachment Page in TwentyEleven Theme?

This is a WP/SEO question related to adding the title attribute to the on an attachment page. We are using the twentyeleven theme and default image.php code, as below.
We want to pull in the title tag (or even automatically set the title for all images, so user doesn't have to individually set, or just use post title for the title - any of these).
The html from View Source looks like this:
The image.php code:
<a href="<?php echo esc_url( $next_attachment_url ); ?>" title="<?php the_title_attribute(); ?>" rel="attachment"><?php
$attachment_size = apply_filters( 'twentyeleven_attachment_size', 848 );
echo wp_get_attachment_image( $post->ID, array( $attachment_size, 1024 ) ); // filterable image width with 1024px limit for image height.
?></a>
I also wanted all my attachments to automatically have a title attribute, so I wrote the following PHP code:
<?php if(wp_attachment_is_image($post->id)) : $att_image = wp_get_attachment_image_src($post->id, "full"); ?><p class="attachment"><a class="show_image" rel="bookmark" href="<?php echo wp_get_attachment_url($post->id); ?>" title="<?php echo get_the_title(); ?>"><img class="img-responsive attachment-medium" src="<?php echo $att_image[0]; ?>" height="<?php echo $att_image[2];?>" width="<?php echo $att_image[1];?>" alt="<?php echo get_the_title(); ?>"></a></p><?php endif; ?>
This PHP code has been tested on WordPress 3.7.1. A live example of this code being used on a custom WordPress image.php file can be found on my Web Development & Design Blog

showing an image from post in wordpress

I am working on a wordpress website. I have different category for one type of posts (call flowers for example). Suppose I have 10 posts in this category. I want to show 10 Images from these posts at home page (one image from one post )
After clicking on the image user will be redirected to the corresponding post.
Is this feasible in wordpress ? SHould i need to use custom fields or something which i found after googling.
Thanks
Setup a loop and query_posts() by the category that they are by the cat id
query_posts( 'cat=1' );
Then you could display a featured image for the post in your homepage loop, as so:
<?php if ( has_post_thumbnail() )
{
$image_id = get_post_thumbnail_id();
$alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true);
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];
?>
<a href="<?php the_permalink(); ?>">
<img src='<?php echo $image_url;?>' alt='<?php echo $alt_text; ?>' title='<?php echo $alt_text; ?>' />
</a>
<?php
}
else
{
?>
<a href="<?php the_permalink(); ?>">
<img src='<?php bloginfo('template_url'); ?>/images/no_image.jpg' />
</a>
<?php
}
?>

Categories