WooCommerce - Custom thumbnails and default fall back image placeholder - php

I just want to insert a wrapper to the woocommerce_get_product_thumbnail I can see that my wrapper appeared but It doesn't have a fall back image if there's no image.
How do I output the default woocommerce thumbnail?
This is my incomplete code:
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
function woocommerce_template_loop_product_thumbnail() {
echo woocommerce_get_product_thumbnail();
}
}
if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {
function woocommerce_get_product_thumbnail( $size = 'shop_catalog', $placeholder_width = 0, $placeholder_height = 0 ) {
global $post, $woocommerce;
$output = '<div class="col-lg-4">';
if ( has_post_thumbnail() ) {
$output .= get_the_post_thumbnail( $post->ID, $size );
} else {
how to show the default woocommerce thumb
}
$output .= '</div>';
return $output;
}
}

I have add an init hook for remove_action & add_action at the beginning to fire it when WordPress/WooCommerce Initialises…
As $placeholder_width = 0, $placeholder_height = 0 is deprecated since woocommerce version 2.0 (see this). You don't need them anymore and it could be part of your problem.
Note: The answer of pallavi is correct for $output .= wc_placeholder_img( $size ); in your else statement. I have already up voted him for this…
So I have changed a little bit your code:
add_action('init', function(){
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
});
if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
function woocommerce_template_loop_product_thumbnail() {
echo woocommerce_get_product_thumbnail();
}
}
if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {
function woocommerce_get_product_thumbnail( $size = 'shop_catalog' ) {
global $post, $woocommerce;
$output = '<div class="col-lg-4">';
if ( has_post_thumbnail() ) {
$output .= get_the_post_thumbnail( $post->ID, $size );
} else {
$output .= wc_placeholder_img( $size );
// Or alternatively setting yours width and height shop_catalog dimensions.
// $output .= '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" width="300px" height="300px" />';
}
$output .= '</div>';
return $output;
}
}

Try below code
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
function woocommerce_template_loop_product_thumbnail() {
echo woocommerce_get_product_thumbnail();
}
}
if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {
function woocommerce_get_product_thumbnail( $size = 'shop_catalog', $placeholder_width = 0, $placeholder_height = 0 ) {
global $post, $woocommerce;
$output = '<div class="col-lg-4">';
if ( has_post_thumbnail() ) {
$output .= get_the_post_thumbnail( $post->ID, $size );
} else {
$output .= wc_placeholder_img( $size );
}
$output .= '</div>';
return $output;
}
}

Related

Please what Am i doing wrong, getting Notice of Undefined variable in PHP 8

I got a Warning message on my WordPress site trying to insert a featured image after the first paragraph. It worked perfectly without warning or error in PHP7.4 but got the below warning on PHP 8.1
Please would need some help thanks in advance.
Warning: Undefined variable $post in /www/wwwroot/.../wp-content/themes/..../functions.php on line 7
Warning: Attempt to read property "ID" on null in /www/wwwroot/.../wp-content/themes/...../functions.php on line 7
add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
$feat_img = get_the_post_thumbnail($post->ID, 'post-single');
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( '<div class="top-featured-image">' . $feat_img . '</div>', 1, $content );
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
You're trying to access a $post variable through $post->ID tho you didn't define it before.
We could first fetch the post object through get_post() then retrieve the ID, but we can do better. We can use get_the_ID() to retrieve the post ID.
<?php
add_filter( 'the_content', function ( $content ) {
if ( ! is_admin() && is_single() ) {
$thumbnail = get_the_post_thumbnail( get_the_ID(), 'post-single' );
return prefix_insert_after_paragraph( '<div class="top-featured-image">' . $thumbnail . '</div>', 1, $content );
};
return $content;
}, 20 );

Append post thumbnail timage to the first content heading tag

I want to add new conditions to the following code:
function add_thumb_after_h2( $content ) {
if (is_single()) {
global $post;
if ( substr_count( $content, '<h2>' ) > 3 ) {
$thumb = '<div id="post_thumbnail">' . get_the_post_thumbnail( $post->ID, 'fullsize' ) . '</div>';
$content = str_replace_once("</h2>", "</h2>".$thumb, $content);
}
}
return $content;
}
I also need to include h3, h4 and h5, example:
if ( substr_count( $content, '<h2>,<h3>,<h4>,<h5>' ) > 3 ) {}
How can I do it?
The thumbnail should be added only to the first occurrence of a heading tag, regardless of its type (h2, h3, etc.)
Thanks
You can achieve this my modifying your function to the following:
function add_thumb_after_h2( $content ) {
if ( is_single() ) {
global $post;
foreach ( array('h2', 'h3', 'h4', 'h5') as $heading ) {
if ( ! $replaced && substr_count( $content, "<{$heading}>" ) > 3 ) {
$thumb = '<div id="post_thumbnail">' . get_the_post_thumbnail( $post->ID, 'fullsize' ) . '</div>';
$content = str_replace_once("</{$heading}>", "</{$heading}>" . $thumb, $content);
break;
}
}
}
return $content;
}

Trying to modify "Read more" link in parent theme

My parent them has a custom "Read more" function. I want to change the text and add to my child theme function.php.
Here is parent them function
add_filter( 'excerpt_length', 'newslite_excerpt_length', 999 );
if ( ! function_exists( 'newslite_implement_read_more' ) ) :
function newslite_implement_read_more( $more ) {
$flag_apply_excerpt_read_more = apply_filters( 'newslite_filter_excerpt_read_more', true );
if ( true !== $flag_apply_excerpt_read_more ) {
return $more;
}
$output = $more;
$read_more_text = __('continue reading','newslite');
if ( ! empty( $read_more_text ) ) {
$output = ' <div class="read-more-text">' . esc_html( $read_more_text ) . '</div>';
$output = apply_filters( 'newslite_filter_read_more_link' , $output );
}
return $output;
}
endif;
add_action( 'excerpt_more', 'newslite_implement_read_more' );
I would just remove their function, and create your own custom one.
add_action( 'excerpt_more', 'custom_newslite_implement_read_more', 9999 );
function custom_newslite_implement_read_more( $more ) {
remove_filter( 'excerpt_length', 'newslite_excerpt_length', 999 );
$flag_apply_excerpt_read_more = apply_filters( 'custom_newslite_filter_excerpt_read_more', true );
if ( true !== $flag_apply_excerpt_read_more ) {
return $more;
}
$output = $more;
$read_more_text = __('read more','newslite'); // change read more to whatever you want
if ( ! empty( $read_more_text ) ) {
$output = ' <div class="read-more-text">' . esc_html( $read_more_text ) . '</div>';
$output = apply_filters( 'custom_newslite_filter_read_more_link' , $output );
}
return $output;
}
Try code:
function new_excerpt_more($more) {
global $post;
return '… ' . 'Read More »' . '';
}
add_filter('excerpt_more', 'new_excerpt_more');
I copied the parent function (everything inside the if statement) to my child functions.php file. then simply changed the text string. This worked.
function newslite_implement_read_more( $more ) {
$flag_apply_excerpt_read_more = apply_filters( 'newslite_filter_excerpt_read_more', true );
if ( true !== $flag_apply_excerpt_read_more ) {
return $more;
}
$output = $more;
$read_more_text = __('More','newslite');
if ( ! empty( $read_more_text ) ) {
$output = ' <div class="read-more-text">' . esc_html( $read_more_text ) . '</div>';
$output = apply_filters( 'newslite_filter_read_more_link' , $output );
}
return $output;
}

Woocommerce category text

I am trying to upgrade my SEO on my webshop. Anyone knows php could fix this puzzle. How can i fit this in each other?
How does this: is_product_category( 'shirts' ) fit in this:
function woa_content_before_shop() {
echo "<p>Insert your Content here...</p>";
}
add_action( 'woocommerce_before_shop_loop', 'woa_content_before_shop');
WordPress conditional tags like is_product_category() can be used to change the displayed content based on the matched condition.
In this case you can use them to change the printed text based on category. You can exploit them in this way:
function woa_content_before_shop()
{
if ( is_product_category( 'shirts' ) )
{
echo "<p>Here are shirts!</p>";
}
else if ( is_product_category( 'games' ) )
{
echo "<p>Here are games!</p>";
}
else
{
...
}
}
add_action( 'woocommerce_before_shop_loop', 'woa_content_before_shop');
Use this code. Also set your descriptions in Products > Categories.
function woa_content_before_shop() {
if ( is_product_category() ) {
global $wp_query;
$id = $wp_query->get_queried_object_id();
$desc = term_description( $id, 'product_cat' );
if ( !empty( $desc ) ) {
$output = '<p>' . esc_html( $desc ) . '</p>';
echo $output;
}
}
}
add_action( 'woocommerce_before_shop_loop', 'woa_content_before_shop');

Adding caption area to 'Flexslider Plug-in'

I have this question on the Wordpress stack exchange as well, but not having any luck there. So, as my solution probably involves me hard-coding php and css, It may be better to have it here.
I'm using 'Flex Slider' plugin - that works on top of 'WP rotator' plug-in on my Wordpress 3.2 website. I have it implemented fine, and beginning to look at inserting my content - but I need to add a caption to be on top of the slider. As are present on most sliders on the web, within the documentation of the non-Wordpress plugin of the tool it suggests I can do something like;
<div class="flex-container">
<div class="flexslider">
<ul class="slides">
<li>
<img src="slide1.jpg" />
<p class="flex-caption">Captions and cupcakes. Winning combination.</p>
</li>
<li>
<img src="slide2.jpg" />
<p class="flex-caption">This image is wrapped in a link!</p>
</li>
<li>
<img src="slide3.jpg" />
</li>
</ul>
</div>
</div>
Problem is; with the Wordpress plug-in version, I can't find that markup to work inside.
Here's the only non-css non-js file in the plug-ins directory, so I assume I have to work in there.
I've tried inserting the mark-up that was suggested non-Wordpress above, but not sure where to insert it as it's broke it with my attempts thus far.
<?php
/*
Plugin Name: Flex Slider for WP Rotator
Plugin URI: http://wordpress.org/extend/plugins/flex-slider-for-wp-rotator/
Description: Turns WP Rotator into FlexSlider, a fully responsive jQuery slider.
Version: 1.1
Author: Bill Erickson
Author URI: http://www.billerickson.net/blog/wordpress-guide
*/
class BE_Flex_Slider {
var $instance;
function __construct() {
$this->instance =& $this;
register_activation_hook( __FILE__, array( $this, 'activation_hook' ) );
add_action( 'plugins_loaded', array( $this, 'init' ) );
}
/**
* Activation Hook
* Confirm WP Rotator is currently active
*/
function activation_hook() {
if( !function_exists( 'wp_rotator_option' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( sprintf( __( 'Sorry, you can’t activate unless you have installed WP Rotator', 'flex-slider-for-wp-rotator'), 'http://wordpress.org/extend/plugins/wp-rotator/' ) );
}
}
function init() {
// Remove original scripts and styles
remove_action('wp_head','wp_rotator_css');
remove_action('admin_head','wp_rotator_css');
remove_action('wp_head','wp_rotator_javascript');
remove_action('admin_head','wp_rotator_javascript');
remove_action('init','wp_rotator_add_jquery');
remove_action('admin_init','wp_rotator_add_jquery');
// Enqueue Scripts and Styles
add_action( 'init', array( $this, 'enqueue_scripts_and_styles' ) );
// Remove original outer markup
remove_action( 'wp_rotator', 'wp_rotator' );
// Add new markup
add_action( 'wp_rotator', array( $this, 'flex_slider' ) );
remove_shortcode( 'wp_rotator' );
add_shortcode( 'wp_rotator', array( $this, 'flex_slider_markup' ) );
}
function enqueue_scripts_and_styles() {
// Use this filter to limit where the scripts are enqueued.
$show = apply_filters( 'be_flex_slider_show_scripts', true );
if ( true === $show ) {
wp_enqueue_style( 'flex-slider', plugins_url( 'flexslider.css', __FILE__ ) );
wp_enqueue_script( 'jquery ');
wp_enqueue_script( 'flex-slider', plugins_url( 'jquery.flexslider-min.js', __FILE__ ), array( 'jquery' ) );
add_action( 'wp_head', array( $this, 'flex_slider_settings' ) );
}
}
function flex_slider_settings() {
?>
<script type="text/javascript" charset="utf-8">
jQuery(window).load(function() {
jQuery('.flexslider').flexslider({
<?php
$flex_settings = array(
'animation' => '"' . wp_rotator_option( 'animate_style' ) . '"',
'slideshowSpeed' => wp_rotator_option( 'rest_ms' ),
'animationDuration' => wp_rotator_option( 'animate_ms' ),
);
$flex_slide_settings = array(
'controlsContainer' => '".flex-container"'
);
if( 'slide' == wp_rotator_option( 'animate_style' ) )
$flex_settings = array_merge( $flex_settings, $flex_slide_settings );
$flex_settings = apply_filters( 'be_flex_slider_settings', $flex_settings );
foreach ( $flex_settings as $field => $value ) {
echo $field . ': ' . $value . ', ';
}
?>
});
});
</script>
<?php
}
function flex_slider_markup() {
$output = '';
if( 'slide' == wp_rotator_option( 'animate_style' ) )
$output .= '<div class="flex-container">';
$output .= '<div class="flexslider"><ul class="slides">';
$loop = new WP_Query( esc_attr( wp_rotator_option('query_vars') ) );
while ( $loop->have_posts() ): $loop->the_post(); global $post;
$url = esc_url ( get_post_meta( $post->ID, 'wp_rotator_url', true ) );
if ( empty( $url ) ) $url = get_permalink($post->ID);
$show_info = esc_attr( get_post_meta( $post->ID, 'wp_rotator_show_info', true ) );
if ( true == $show_info ) {
$title = get_the_title();
if ( get_the_excerpt() ) $excerpt = get_the_excerpt();
else $excerpt = '';
$caption = $title . ' <span class="excerpt">' . $excerpt . '</span>';
$info = '<p class="flex-caption">' . apply_filters( 'be_flex_slider_caption', $caption, $title, $excerpt ) . '</p>';
} else {
$info = '';
}
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'wp_rotator' );
$slide = '<li><img src="' . $image[0] . '" />' . $info . '</li>';
$output .= apply_filters( 'be_flex_slider_slide', $slide );
endwhile; wp_reset_query();
$output .= '</ul></div>';
if( 'slide' == wp_rotator_option( 'animate_style' ) )
$output .= '</div>';
return $output;
}
function flex_slider() {
echo $this->flex_slider_markup();
}
}
new BE_Flex_Slider;
?>
I have contacted the plug-in developer, he's not responding so I assume hes not going to support my question - so I'm left to handcode.
http://wordpress.org/extend/plugins/wp-rotator/
http://flex.madebymufffin.com/
http://wordpress.org/extend/plugins/flex-slider-for-wp-rotator/
Thanks for any pointers!
It looks like captions are automatically added to the slider as long as you set the post to show rotator info (wp_rotator_show_info... probably on the plugin settings page or on your individual post page). The automatic caption is made up of the title of the post plus the excerpt. Here's the key part in the plugin above:
$show_info = esc_attr( get_post_meta( $post->ID, 'wp_rotator_show_info', true ) );
if ( true == $show_info ) {
$title = get_the_title();
if ( get_the_excerpt() ) $excerpt = get_the_excerpt();
else $excerpt = '';
$caption = $title . ' <span class="excerpt">' . $excerpt . '</span>';
$info = '<p class="flex-caption">' . apply_filters( 'be_flex_slider_caption', $caption, $title, $excerpt ) . '</p>';
} else {
$info = '';
}
UPDATE: If you want the caption to show no matter what, replace the above portion with this:
$title = get_the_title();
if ( get_the_excerpt() ) $excerpt = get_the_excerpt();
else $excerpt = '';
$caption = $title . ' <span class="excerpt">' . $excerpt . '</span>';
$info = '<p class="flex-caption">' . apply_filters( 'be_flex_slider_caption', $caption, $title, $excerpt ) . '</p>';
Note that I merely deleted the part that checks for wp_rotator_show_info.

Categories