Custom attributes on catalog page? (5 posts)

  1. aand
    Posted 3 years ago #

    I'd like to add a custom attribute text to each product on the catalog page (a tagline), and as I understand it, I need to use the following php below:

    <?php echo $_product->get_attribute( 'tagline' );?>

    (tagline being the name of the attribute I've set)

    However, this produces an error, presumably because not all products have this attribute set.

    So I'm guessing that I need to add some additional code to check whether the given attribute is available, before the php above.

    Any pointers on how to to do this?


  2. Roy Ho
    Posted 3 years ago #

    Yes the product class has a method called "has_attributes" which you can use.

  3. aand
    Posted 3 years ago #

    Ah thanks, sounds good - any pointers on the php for this (my php skills are very limited)?

  4. Roy Ho
    Posted 3 years ago #

    has_attributes method returns a boolean true or false...So if your new class is called $_product assuming you used the abstract method of $_product = get_product( $id ); then your code would look something like this:

    if ( $_product->has_attributes() ) {
         echo $_product->get_attribute( 'tagline' );
  5. aand
    Posted 3 years ago #

    Much appreciate - will give that a go!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic