3

I'm trying to get a product attribute on the product listing page, on title.phtml.

I've tried quite a few different methods found through googling but nothing seems to be working.

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * @var $block \Magento\Theme\Block\Html\Title
 */
$cssClass = $block->getCssClass() ? ' ' . $block->getCssClass() : '';
$title = '';
if (trim($block->getPageHeading())) {
    $title = '<span class="base" data-ui-id="page-title-wrapper" ' . $block->getAddBaseAttribute() . '>'
        . $block->escapeHtml($block->getPageHeading()) . '</span>';
}
?>
<?php if ($title): ?>
    <div class="page-title-wrapper<?php /* @escapeNotVerified */
    echo $cssClass; ?>">
        <h1 class="page-title"
            <?php if ($block->getId()): ?> id="<?php /* @escapeNotVerified */
            echo $block->getId(); ?>" <?php endif; ?>
            <?php if ($block->getAddBaseAttributeAria()): ?>
                aria-labelledby="<?php /* @escapeNotVerified */
                echo $block->getAddBaseAttributeAria(); ?>"
            <?php endif; ?>>
            <?php /* @escapeNotVerified */
            echo $title ?>
        </h1>
        testing:
        <?php
        $attr = $block->getData('style_general');
        echo $attr;
        ?>
        <?php echo $block->getStyle_general(); ?>
        <?php echo $block->getChildHtml(); ?>
    </div>
<?php endif; ?>

Testing:

$attr = $block->getData('style_general');
echo $attr; 

is the current method I'm trying, but it nothing is showing up...

4
  • You want to get custom attribute on product listing vendor\magento\module-catalog\view\frontend\templates\product\list.phtml?. I don't know why you want to get custom attribute in vendor\magento\module-theme\view\frontend\templates\html\title.phtml Commented Jul 4, 2016 at 13:08
  • because I want to put a 'sub title' under the title Commented Jul 4, 2016 at 17:54
  • Could you please accept the answer that has worked for you as the correct one?
    – Max
    Commented Oct 27, 2016 at 14:31
  • Try magento.stackexchange.com/questions/194014/… or share which way has worked for you. Please, share if you solved the issue.
    – Gediminas
    Commented May 4, 2018 at 15:27

2 Answers 2

1

Generally, you can get custom attributes with $_product->getData('custom_attribute');, depending on which Interceptor is used right now.

For example, you can't call your custom attribute when showing Upselling products, since the Interceptor doesn't have access to it.

But that's not all. If you are using a Dropdown field, or a Multiselect, you will get the ID based of the selected value as a string back. In order to get the correct labels for those selected strings you need to the following:

$_product->getResource()->getAttribute('your_attribute_code')->getFrontend()->getValue($_product)

Just replace your_attribute_code with whatever your attribute is named.

1

Add Below code in your list.phtml file in Magento 2. Set your Attribute code in attribute name. After this you will get attribute label or value.

$attr = $_item->getResource()->getAttribute('Yourattributecode')->getOptions();
foreach ($attr as $attrColor) {
    if ($attrColor->getValue()) {
        echo $attrColor->getLabel();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.