I have created two phtml files that can be called from within a CMS Block on a product page to echo the product name and an attribute name.
As follows:
Product Name
{{block class="Vendor\ProductName\Block\ProductName" template="Vendor_ProductName::productname.phtml" }}
Attribute Name (Artist)
{{block class="Magento\Catalog\Block\Product\View" name="artist" template="Magento_Catalog::artist.phtml"}}
I want to be able to create a title within the H1 tag for the product name, as follows
Buy 'Product name' by 'Artist'
The following is the title.phtml code thats currently used.
<?php
/**
* Copyright © Magento, Inc. 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): ?>
<?php if ($this->getRequest()->getFullActionName() == 'catalog_product_view'): ?>
<h1 class="tt-title"><?= /* @escapeNotVerified */ $title ?></h1>
<?php else: ?>
And this is how it outputs on the frontend
<h1 class="tt-title"><span class="base" data-ui-id="page-title-wrapper" itemprop="name">Terrace of a café at night (Place du Forum)</span></h1>
Is there a way or using the phtml calls that I use in the CMS Blocks to create the product page H1 title?
UPDATE
I used the following to get part of the title
Buy "Product Name" by
By using the following code,
Changed the H1 Title as follows
title="Buy <?php echo $block->getPageHeading(); ?> by">
This is the code I'm using to get the current product page attribute
<?php
$product = $block->getProduct();
$product_name = $product->getName();
$attr_exists = $product->getResource()->getAttribute('artist');
if ($attr_exists){
$attr_name = $product->getAttributeText('artist');
}
echo $product->getAttributeText('artist');
?>
How can I use the above code to add the artist name to the H1 title?