admin管理员组

文章数量:1287831

I am using the following code for displaying a custom attribute on the Woocommerce shop page. But for some reason, the year is displayed above the product image instead of after the product title (In between the product title and the price).

add_action('woocommerce_after_shop_loop_item_title', 'YearOfMake', 10);

function YearOfMake()
{
    global $product;

    $abv = $product->get_attribute('year-made');
    if (empty($abv))
        return;
    echo __($abv, 'woocommerce');
}

Link: /

I am using the following code for displaying a custom attribute on the Woocommerce shop page. But for some reason, the year is displayed above the product image instead of after the product title (In between the product title and the price).

add_action('woocommerce_after_shop_loop_item_title', 'YearOfMake', 10);

function YearOfMake()
{
    global $product;

    $abv = $product->get_attribute('year-made');
    if (empty($abv))
        return;
    echo __($abv, 'woocommerce');
}

Link: https://groovygarbs/cars/

Share Improve this question edited Jan 29, 2019 at 11:18 pikaaaaaaaaa asked Jan 28, 2019 at 23:43 pikaaaaaaaaapikaaaaaaaaa 11 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1
add_filter( 'the_title', 'YearOfMake', 10, 2 );
function YearOfMake( $title, $post_id ) 
{

    if ( get_post_type( $post_id ) != 'product' && ! is_archive() )
        return $title;

    if ( ! ( is_shop() || is_product_category() || is_product_tag() ) )
        return $title;
    /*
        global $product;
        $attribute  = 'year-made';
        $abv        = $product->get_attribute($attribute);
        if(!empty($abv))
        {
            $title .= '<br /><p>' . $abv . '</p>';
        }
    */
    $terms  = wp_get_post_terms( $post_id, 'product_tag' );
    $term   = reset( $terms );
    if ( !empty( $term->name ))
    {
        $title .= '<br /><p>' . strtoupper( $term->name ) . '</p>';
    }
    return $title;
}
add_action('woocommerce_after_shop_loop_item_title', 'YearOfMake', 15);
function YearOfMake()
{
    global $product;
    $attribute  = 'year-made';
    $abv        = $product->get_attribute($attribute);
    if(!empty($abv))
    {
        echo '<p>' . $abv . '</p>';
    }
}

本文标签: phpDisplay attribute on shop page after the title