I have found the following code from this answer thread, but it only applied under the product title. So how can apply to the detailed description of the product.
add_action( 'woocommerce_after_shop_loop_item_title', 'shorten_product_excerpt', 35 );
function shorten_product_excerpt()
{
global $post;
$limit = 14;
$text = $post->post_excerpt;
if (str_word_count($text, 0) > $limit) {
$arr = str_word_count($text, 2);
$pos = array_keys($arr);
$text = substr($text, 0, $pos[$limit]) . '...';
// $text = force_balance_tags($text); // may be you dont need this…
}
echo '<span class="excerpt"><p>' . $text . '</p></span>';
}
In Woocommerce product single pages, the long description is displayed in the "description tab". If you look at the source code of single-product/tabs/description.php template, it use the_content() wordpress function to display that long description.
So you can use the_content dedicated Wordpress filter hook to reduce the product long description:
add_filter( 'the_content', 'shorten_product_long_descrition', 20 );
function shorten_product_long_descrition( $content ){
// Only for single product pages
if( ! is_product() ) return $content;
// Set the limit of words
$limit = 14;
if (str_word_count($content, 0) > $limit) {
$arr = str_word_count($content, 2);
$pos = array_keys($arr);
$text = '<p>' . substr($content, 0, $pos[$limit]) . '...</p>';
$content = force_balance_tags($text); // needded
}
return $content;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Before:
After:
Similar: Limit product short description length in Woocommerce
Related
i need to add product link url (of external affiliate) under long description in woocommerce single product page. how to do? thank you
I put this code in functions.php but it doesn't print the url near the description.
add_filter( 'the_content', 'shorten_product_long_descrition', 20 );
function shorten_product_long_description( $content ){
// Only for single product pages
if( ! is_product() ) return $content;
// Set the limit of words
$limit = 13;
if (str_word_count($content, 0) > $limit) {
$arr = str_word_count($content, 2);
$pos = array_keys($arr);
$text = '<p>' . substr($content, 0, $pos[$limit]) . "..<a href='" . esc_url( $product_url ) . "' class='goto_more_offer_tab button'>...continue reading</a></p>";
$content = force_balance_tags($text); // needed
}
return $content;
}
One can control the WP default (Automatic) excerpt length of a WP post using the using the following snippet within functions.php;
From the WP Codex
// . Post excerpt adjustment (Auto)
// . ==============================
function wpdocs_custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );
My question is how do you limit the manual one?
You know, the exerpt specifically added by the user themselves?
(*) There is an 8 year old question here, that does provide some context but given the current year and progress WP has made I want to post the question again and receive some clarity on the subject.
Added Context: (Edited: 12 March 2019)
It's not that the original answer to the question posted earlier doesn't work, all be it seems, really clunky. I'm looking for a more simple & robust answer using exerpt_length filter. Rather than using something like the following to trim the text; (If Possible)
function excerpt($limit) {
return wp_trim_words(get_the_excerpt(), $limit);
}
We have by default in core, the following filtering:
add_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
but within wp_trim_excerpt() the trimming is only applied on the post's content, when there's no manual excerpt set.
Here's an untested suggestion for a custom filtering:
add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
if ( has_excerpt( $post ) ) {
$excerpt_length = apply_filters( 'excerpt_length', 55 );
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
$excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
}
return $excerpt;
}, 10, 2 );
to apply the similar trimming on manual excerpts.
Hope you can adjust this further to your needs.
Try this, I got this code from here: https://www.wpexplorer.com/wordpress-excerpt-length/
add_filter( 'excerpt_length', function($length) {
return 20;
} );
This pair of functions will give you control over the excerpt length, including the manual excerpt which is returned if available, otherwise the "excerpt-ized" post_content gets returned. These go in your theme functions file:
function get_excerpt_by_id($post_id, $length = NULL) {
$length = isset($length) ? $length : apply_filters('excerpt_length', 32);
$p = get_post($post_id);
return $p->post_excerpt ? build_excerpt_by_length($p->post_excerpt, $length) : build_excerpt_by_length($p->post_content, $length);
}
function build_excerpt_by_length($content, $length = 32) {
$excerpt = strip_tags(strip_shortcodes($content));
$words = explode(' ', $excerpt, $length + 1);
$words = array_slice($words, 0, $length);
$result = trim(implode(' ', $words));
$result = preg_replace('/\W*$/', '', $result);
$more = apply_filters('excerpt_more', '…');
if ($result !== '') $result = $content === $result ? $result : $result . $more;
return $result;
}
Then in your templates you can use by calling:
get_excerpt_by_id($your_post_id, $preferred_excerpt_length);
You can try the following code
$excerpt = get_the_excerpt();
$excerpt = substr( $excerpt, 0, 180 );
$excerpt_description = substr( $excerpt, 0, strrpos( $excerpt, ' ' ) );
echo $excerpt_description;
I have a similar problem mentioned here and I want that a short description of the variable product was displayed on the catalog page.
I'm using WooCommerce Show Single Variations commercial pluginn, but it doesn't display short description.
Before that I used some code to display description of simple product on shop page that worked and it looks like this:
add_action( 'woocommerce_after_shop_loop_item_title',
'add_short_description', 9 );
function add_short_description() {
global $post;
$text = $post->post_excerpt;
$maxchar = 75; //максимальное кол-во символов
$text = preg_replace ('~\[[^\]]+\]~', '', $text ); //убираем шорткоды
//удаляем все html символы
//$text = strip_tags( $text);
// Обрезаем
if ( mb_strlen( $text ) > $maxchar ){
$text = mb_substr( $text, 0, $maxchar );
$text = preg_replace('#(.*)\s[^\s]*$#s', '\\1 ...', $text );
}
echo "<span class='catalog-short-desc'>$text</span>";
}
I would be grateful if you tell me how to change this code.
Updated: As I dont have and I don't use your commercial plugin and as your question is not so clear… There is 2 possibilities:
1) You would like to add the short description (from the parent variable product) for the product variations that are displayed (with your plugin) on woocommerce archives pages (like shop …), just like the other products.
This should do the trick (untested with no errors):
add_action( 'woocommerce_after_shop_loop_item_title', 'add_short_description', 9 );
function add_short_description() {
global $post;
// 1. Product variation
if ($post->post_type = 'product_variation'){
$post_parent_id = $post->post_parent; // Get the parent variable product ID
$post_parent = get_post( $post_parent_id ); // Get the parent variable WP_Post object
$text = $post_parent->post_excerpt; // Get the short description from the parent variable product
}
// 2. Other cases (simple, variable …)
else {
$text = $post->post_excerpt;
}
$maxchar = 75; // Maximum number of characters
$text = preg_replace ('~\[[^\]]+\]~', '', $text ); // Remove shortcodes
// Remove all html symbols
//$text = strip_tags( $text);
// Crop
if ( mb_strlen( $text ) > $maxchar ){
$text = mb_substr( $text, 0, $maxchar );
$text = preg_replace('#(.*)\s[^\s]*$#s', '\\1 ...', $text );
}
echo "<span class='catalog-short-desc'>$text</span>";
}
2) You would like to add the description of the product variations that are displayed (with your plugin) on woocommerce archives pages (like shop …), just like the other products… For Product variation short description doesn't exist (it's empty)…
This should do the trick (untested with no errors):
// For Woocommerce versions 3+ only
add_action( 'woocommerce_after_shop_loop_item_title', 'add_short_description', 9 );
function add_short_description() {
global $post, $product;
// 1. Product variation
if ( $product->is_type('product_variation') ){
// Get the product variation description
// short description doesn't exist for product variations
$text = $product->get_description();
// If the product variation description is empty, we get the parent short description
if( empty( $text ) ){
$parent_id = $product->get_parent_id(); // The parent variable product ID
$parent_product = wc_get_product( $parent_id ); // The parent WC_Product object
$text = $parent_product->get_short_description();
}
}
// 2. Other cases (simple, variable …)
else {
$text = $product->get_short_description();
}
$maxchar = 75; // Maximum number of characters
$text = preg_replace ('~\[[^\]]+\]~', '', $text ); // Remove shortcodes
// Remove all html symbols
//$text = strip_tags( $text);
// Crop
if ( mb_strlen( $text ) > $maxchar ){
$text = mb_substr( $text, 0, $maxchar );
$text = preg_replace('#(.*)\s[^\s]*$#s', '\\1 ...', $text );
}
echo "<span class='catalog-short-desc'>$text</span>";
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
I'm currently pulling in multiple pieces of content into my posts through an AFC with the value: "section_content". Furthermore, I'm using the code below to clean up my WP posts. How would I modify this filter to also include my ACF?
<?php
/**
* Clean posts from inline styling and unnecessary tags
*/
add_filter( 'the_content', 'clean_post_content' );
function clean_post_content($content) {
if ( is_single() ) {
$patterns = array(
'/(<[^>]+) style=".*?"/i', // Remove inline styling
'/<\/?font[^>]*>/', // Remove font tag
'/<(p|span)>(?>\s+| |(?R))*<\/\1>/', // Empty p, span (font tags already removed)
'/(<h[1-6]>[^<]*)<\/?strong>(.*?<\/h[1-6]>)/', // h1-6
);
$replacements = array(
'$1',
'',
'',
'$1$2'
);
$old_content = '';
while ($old_content != $content) {
$old_content = $content;
$content = preg_replace($patterns, $replacements, $content);
}
}
return $content;
}
?>
I guess you could use ACF Filters. I usually hook to acf/format_value in order to clean or modify my custom field values before printing them.
You can even hook to certain field types only, like:
function acf_brand_trademark( $value, $post_id, $field ) {
$value = preg_replace( '/Brand /', 'Brand<sup>™</sup> ', $value );
return $value;
}
add_filter('acf/format_value/type=textarea', 'acf_brand_trademark', 10, 3);
add_filter('acf/format_value/type=text', 'acf_brand_trademark', 10, 3);
Hope this helps!
I have successfully added the a short product description to my shop page, but would like it displayed below the thumbnail, rather than below the price and button.
I can accomplish this by reordering my hooks, but when I do this, the description turns bold and takes on the characteristics of the product title.
See difference below
Does anyone know how I can make it so the text displays above the price and button, but isn't bold and a link?
See my functions.php code..
/** Add buy it now to shop page **/
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_add_to_cart', 10 );
/** Add excerpt below thumbnail **/
add_action( 'woocommerce_after_shop_loop_item_title', 'lk_woocommerce_product_excerpt', 35, 2);
if (!function_exists('lk_woocommerce_product_excerpt'))
{
function lk_woocommerce_product_excerpt()
{
$content_length = 20;
global $post;
$content = $post->post_excerpt;
$wordarray = explode(' ', $content, $content_length + 1);
if(count($wordarray) > $content_length) :
array_pop($wordarray);
array_push($wordarray, '...');
$content = implode(' ', $wordarray);
$content = force_balance_tags($content);
$content = substr($content, 0, 140);
endif;
echo "<span class='excerpt'><p>$content</p></span>";
}
}