Wordpress: Separating featured image from products gallery - php

I'm trying to separate the Featured Image from the Products Variation Gallery.
I was able to split the Featured Image, thanks to a custom shortcode placed in function.php, and I even managed to do an on-click action - Lightbox as well as a custom size option.
However, for the past few days I've been having trouble setting up the same feature but for the variation products gallery.
The code that I used to output the featured photo is this:
global $product;
$title = $product->get_name();
$image_id = $product->get_image_id();
$image_url = wp_get_attachment_url( $image_id );
$image_src = wp_get_attachment_image_src( $image_id, array( 740, 360 ) );
echo '<center><a href="' . $image_url . '" data-lightbox="variation-product" data-title="' . $product->get_name() . '">';
echo '<img src="' . $image_src[0] . '" width="' . $image_src[1] . '" height="' . $image_src[2] . '" alt="' . $product->get_name() . '">';
echo '</a></center>';
And this one is for the variation products gallery:
global $product;
$attachment_ids = $product->get_gallery_image_ids();
foreach ( $attachment_ids as $attachment_id ) {
$image_url = wp_get_attachment_url( $attachment_id );
$image_src = wp_get_attachment_image_src( $attachment_id, array( 740, 360 ) );
echo '<center><a href="' . $image_url . '" data-lightbox="variation-product" data-title="' . $product->get_name() . '">';
echo '<img src="' . $image_src[0] . '" width="' . $image_src[1] . '" height="' . $image_src[2] . '" alt="' . $product->get_name() . '">';
echo '</a></center>';
}
And this is how it looks. This is the main problem. I tried to limit the picutres like this:
$i = 0;
foreach ( $attachment_ids as $attachment_id ) {
if ($i == 1) break;
/* the rest of the code */
$i++;
}
and this is the final result. You wonder where the problem is? Well... There is.
This variant of the code completely removes all variation images. The result I am trying to achieve is:
When we have more than one variable image, only the first one is shown on the product page. When the first variation photo is opened in the Lightbox, all other photos should be visible.
How can I do that? The theme that I'm using is Woodmart, by XTemos.

Related

How to display/insert an image on an option html tag

I'm trying to insert some code in WooCommerce wc_dropdown_variation_attribute_options function.
I got line like this:
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name, $term, $attribute, $product ) ) . '</option>';
and I need to insert this code:
'<img src="' . esc_attr($variation['image']['thumb_src']) . '">' .
if I just insert it like that - nothing shows, but when I display it inside esc_html, the code is shown as plain text and I see it configured correctly.
How do I need to insert code here to display image?
I just tested this and it's showing the image.
function woocommerce_dropdown_variation_attribute_options_html( $html, $args ) {
$variation['image']['thumb_src'] = "https://i.picsum.photos/id/230/50/50.jpg";
$html = '<img src="' . esc_attr($variation['image']['thumb_src']) . '">';
$html.= '<img src="' . esc_attr($variation['image']['thumb_src']) . '">';
return $html;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'woocommerce_dropdown_variation_attribute_options_html', 10, 2 );

Display the product category thumbnail on Woocommerce product page

I would like to add on my product the image of the category parent, I manage to recover the description of my category
but not the picture
Any help is highly appreciated.
The following function based on woocommerce_subcategory_thumbnail() source code will display any product category thumbnail from category Id, name or slug:
/**
* Display product category thumbnail.
*
* #param mixed $product_category Category term Id, term name or term slug.
*/
function display_product_category_thumbnail( $product_category ) {
$taxonomy = 'product_cat';
if( term_exists( $product_category, $taxonomy ) ) {
if( is_numeric($product_category) )
$field_type = 'term_id';
else
$field_type = 'slug';
} else
return;
$term = get_term_by( $field_type, sanitize_title( $product_category ), 'product_cat' );
$small_thumb_size = 'woocommerce_thumbnail';
$dimensions = wc_get_image_size( $small_thumb_size );
if ( $thumbnail_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true ) ) {
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumb_size );
$image = $image[0];
$image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumb_size ) : false;
$image_sizes = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumb_size ) : false;
} else {
$image = wc_placeholder_img_src();
$image_srcset = false;
$image_sizes = false;
}
if ( $image ) {
// Prevent esc_url from breaking spaces in urls for image embeds.
// Ref: https://core.trac.wordpress.org/ticket/23605.
$image = str_replace( ' ', '%20', $image );
// Add responsive image markup if available.
if ( $image_srcset && $image_sizes ) {
echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $term->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" srcset="' . esc_attr( $image_srcset ) . '" sizes="' . esc_attr( $image_sizes ) . '" />';
} else {
echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $term->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" />';
}
}
}
Tested and works
Update - Addition (Related to your comments)
To get the product categories from the WC_Product object $product and display it with its description and thumbnail, you will use:
global $product;
$term_ids = $product->get_category_ids();
// Loop through product category IDs
foreach( $term_ids as $term_id ){
$term = get_term ( $term_id, 'product_cat' );
// Product category name
echo '<p>' . $term->name . '</p>';
// Product category description
echo '<p>' . $term->description . '</p>';
// Product category description
display_product_category_thumbnail( $term_id )
}
Tested and works
Note: $product->get_categories(); is outdated and deprecated and gives a list of formatted product categories, which is not really the best effective way.
For info: get_categories() method is replaced by function wc_get_product_category_list()

Change Logo by User Role in Wordpress

Hey guys I have been asked to brand the site for my company. We are using Wordpress for it and basically each User Role is a different group that needs to see a different logo on the site.
I am using Eduma by Thimpress as my theme. I feel like it should be as simple as checking what user_role is logged in and changed the logo image, but I don't know where to start to put that condition.
Any help or guidance is much appreciated!
Below is the code I think creates the logo:
<?php
add_action( 'thim_logo', 'thim_logo', 1 );
// logo
if ( !function_exists( 'thim_logo' ) ) :
function thim_logo() {
$thim_logo = get_theme_mod( 'thim_logo', false );
$style = '';
if ( !empty( $thim_logo ) ) {
if ( is_numeric( $thim_logo ) ) {
$logo_attachment = wp_get_attachment_image_src( $thim_logo, 'full' );
if ( $logo_attachment ) {
$src = $logo_attachment[0];
$style = 'width="' . $logo_attachment[1] . '" height="' . $logo_attachment[2] . '"';
} else {
// Default image
// Case: image ID from demo data
$src = get_template_directory_uri() . '/images/logo.png';
$style = 'width="153" height="40"';
}
} else {
$src = $thim_logo;
}
} else {
// Default image
// Case: The first install
$src = get_template_directory_uri() . '/images/logo-sticky.png';
$style = 'width="153" height="40"';
}
$src = thim_ssl_secure_url($src);
echo '<a href="' . esc_url( home_url( '/' ) ) . '" title="' . esc_attr( get_bloginfo( 'name' ) ) . ' - ' . esc_attr( get_bloginfo( 'description' ) ) . '" rel="home" class="no-sticky-logo">';
echo '<img src="' . $src . '" alt="' . esc_attr( get_bloginfo( 'name' ) ) . '" ' . $style . '>';
echo '</a>';
}
endif;
add_action( 'thim_sticky_logo', 'thim_sticky_logo', 1 );
// get sticky logo
if ( !function_exists( 'thim_sticky_logo' ) ) :
function thim_sticky_logo() {
$sticky_logo = get_theme_mod( 'thim_sticky_logo', false );
$style = '';
if ( !empty( $sticky_logo ) ) {
if ( is_numeric( $sticky_logo ) ) {
$logo_attachment = wp_get_attachment_image_src( $sticky_logo, 'full' );
if ( $logo_attachment ) {
$src = $logo_attachment[0];
$style = 'width="' . $logo_attachment[1] . '" height="' . $logo_attachment[2] . '"';
} else {
// Default image
// Case: image ID from demo data
$src = get_template_directory_uri() . '/images/logo-sticky.png';
$style = 'width="153" height="40"';
}
} else {
$src = $sticky_logo;
}
} else {
// Default image
// Case: The first install
$src = get_template_directory_uri() . '/images/logo-sticky.png';
$style = 'width="153" height="40"';
}
$src = thim_ssl_secure_url($src);
echo '<a href="' . esc_url( home_url( '/' ) ) . '" rel="home" class="sticky-logo">';
echo '<img src="' . $src . '" alt="' . esc_attr( get_bloginfo( 'name' ) ) . '" ' . $style . '>';
echo '</a>';
}
endif;
In the file where you set the logo put this code there.
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
//logo for author role
}
That depends on the layout of the theme. Likely, it's in the header.php.
The way to proceed is something like this:
Create a child theme
https://codex.wordpress.org/Child_Themes
Find out what file outputs the link to the logo
Probably header.php but that depends on the theme; ask the theme developer if unsure.
Use for example wp_get_current_user()
https://codex.wordpress.org/Function_Reference/wp_get_current_user
Do a php switch on the outcome of that and output various links
http://php.net/manual/en/control-structures.switch.php

Permalink of specific post ID

Below the code I have on my page tot display the permalink of q specific post in Wordpress. It works, but I have the feeling it can be easier. Can somebody explain how?
$post_id = 26; // post id
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
$content = $queried_post->post_content;
$perma = get_permalink($post_id);
if ( has_post_thumbnail() ) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ );
}
echo '<a href="' . $perma . '" title="' . $title . '">';
echo $title;
echo '</a>';
echo '<img width="100%" src="' . $image_src[0] . '">';
echo $content;
Well your code seems ok. However there are some improvements that can be made.
For example, when you have the $queried_post object, you don't need to create additional variables for content and title. You can use this object properties to get the values.
Also you can use wordpress get_the_post_thumbnail to show featured image.
Some formatting and it's almost perfect.
$post_id = 26; // post id
$queried_post = get_post($post_id);
echo '<a href="' . get_permalink( $post_id ) . '" title="' . $queried_post->post_title . '">';
echo $queried_post->post_title;
echo '</a>';
if ( has_post_thumbnail( $post_id ) ) {
echo get_the_post_thumbnail( $post_id, 'full', array('width' => '100%') );
}
echo $queried_post->post_content;
try this code
$post_id = 26;
if ( has_post_thumbnail($post_id) )
{
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_id),’thumbnail’ );
}
echo '<a href="' . get_the_permalink($post_id) . '" title="' .get_the_title($post_id). '">';
echo get_the_title($post_id);
echo '</a>';
echo '<img width="100%" src="' . $image_src[0] . '">';
echo get_the_content($post_id);
In response to your comment, if you want to simplify this line:
echo '<a href="' . $perma . '" title="' . $title . '">';
echo $title;
echo '</a>';
You could do:
echo '' . $title . '';
So replacing multiple echo; with the "." to 'continue' without breaks.

wordpress : how to change the display of meta on single custom post type

I found code for displaying the meta terms of a custom post type.
but it shows the metas as a vertical list.
I want the code to show CPT meta in a row, instead.
any help please?
the code:
<?php
//get all taxonomies and terms for this post (uses post's post_type)
foreach ( (array) get_object_taxonomies($post->post_type) as $taxonomy ) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo '- '.$taxonomy;
foreach ($object_terms as $term) {
echo '<p><a href="' . esc_attr(get_term_link($term, $taxonomy)) .
'" title="' . sprintf( __( "View all posts in %s" ), $term->name ) .
'" ' . '>' . $term->name.'</a><p> ';
}
}
}
?>
It's because of the HTML markup in your foreach loop, try removing the paragraph tags to something like:
foreach ($object_terms as $term) {
echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
}

Categories