phpwordpresswoocommerce

Wordpress: excerpt only for Woocommerce Brand pages


Dear StackOverflow friends, in a Wordpress E-store, we use Woocommerce and its extension plugin Woocommerce Brand Addon.

I'd like this code (located in functions.php) to be applied only to Woocommerce Brand pages: the code does its job in Woocommerce Brands but it is applied also in other categories/archives

add_action( 'woocommerce_after_shop_loop_item_title', 'lk2_woocommerce_product_excerpt', 35, 2);
if (!function_exists('lk2_woocommerce_product_excerpt'))
{
function lk2_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);
endif;
echo "<span class='excerpt'><p>$content</p></span>";
}
}

I played with 'taxonomy' and 'include', inserting ID of Brands, but no results. Here is my last attempt.

add_action( 'woocommerce_after_shop_loop_item_title', 'lk2_woocommerce_product_excerpt', 35, 2);
if (!function_exists('lk2_woocommerce_product_excerpt'))
{
function lk2_woocommerce_product_excerpt()
{
$content_length = 20;
global $post;
$args = array(
'include'            => '120,121,122,123,124,125,126,127',
'taxonomy'           => 'product_brand',
); 
$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);
endif;
echo "<span class='excerpt'><p>$content</p></span>";
}
}

Unfortunately we haven't bought a domain yet, so I can't show you a link.

I could fix the output with css, but I'd prefer to solve directly from the code. Can you help me in finding the direction to fix my error? Thanks for your time!


Solution

  • Thanks for all your directions! Here is the code, thanks to David

    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 = 10;
            if(get_query_var('product_brand'))
                $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);
            endif;
            echo "<span class='excerpt'><p>$content</p></span>";
        }
    }