我如何从 magento 2 的属性值中获取属性标签?

how can i get the the attribute label from the attribute value in magento 2?

我从下面的代码中得到了属性值 232

$_product->getData('farben');

我如何从这个值得到它的标签?

请试试这个代码

第 1 步)首先您必须加载产品

$_productCollection = $block->getLoadedProductCollection();

第2步)在产品列表页面中,将有一个foreach循环用于列出这样的产品

foreach ($_productCollection as $_product)

第 3 步)您的代码将位于此 loop.Place 以下代码中您想要显示属性标签的任何位置。

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

只需将 your_attribute_code 替换为您的属性命名即可。

试试

$_product->getAttributeText('farben');
$passing_marks = $_product->getResource()->getAttribute('attribute_code');

$passing_marks->getFrontend()->getLabel($_product);

试试这个

<?php $_product = $block->getProduct(); ?>
<?php if (null !== $_product->getCustomAttribute('attribute_code')): ?>
    <?= $_product->getAttributeText('attribute_code'); ?>
<?php endif; ?>