Magento 2: How to display custom attribute in catalog?

We can get custom attribute value like below

Attribute Value

<?php /* @escapeNotVerified */ echo $_product->getResource()->getAttribute('c_address')->getFrontend()->getValue($_product); ?>

Label

$address =$_product->getResource()->getAttribute('c_address')->getStoreLabel();

Note : in above line c_address is my custom attribute code.

Reference: Magento/Catalog/templates/product/view/attribute.phtml


I created an article about this, that have only 5 steps to do it:

1 - Create your attribute in Stores > Attributes > Product.

2 - Set the attribute in the Default group, go to Stores > Attributes > Attribute Set.

3 - Set the values of this attributes in your products.

4 - Open in your custom theme the relative file below:

app/design/frontend/CUSTOM/THEME/Magento_Catalog/templates/product/list.phtml

5 - Between the foreach function paste this code below, but changing the code of your correct attribute:

$_getMyAttr = $_product->getResource()->getAttribute('my_attribute');

if ($_getMyAttr){

    // Get Value
    $attrTestValue = $_getMyAttr->getFrontend()->getValue($_product);

    // Get Label
    $attrTestLabel = $_getMyAttr->getStoreLabel();
}

Important

Make sure “Visible on Product View Page on Front-end” and “Used in Product Listing” options under storefront Properties are set to Yes.

Reference: https://rafaelstz.github.io/magento/magento2-display-custom-attribute-catalog-list-products.html


Use this

$attribute = $_product->getResource()->getAttribute('identifier'); 
if ($attribute) 
{ 
$attr_value = $attribute ->getFrontend()->getValue($_product); 
}