In this article we would like to share with the community one of the ways to display a custom CMS attribute on the Magento frontend. In my previous article I have explained how to add a CMS attribute at the backend. Check it out here (Adding a custom attribute to the CMS page).
There is no way to display an attribute without calling getAttribute or getData methods in the template, but we can also create our own template as an option.
First of all, you should create your custom layout. Then add the following code to your config.xml under the <global> scope:
<page> <layouts> <atwix_custom_page> <label>Atwix Custom Page</label> <template>page/atwix-page.phtml</template> </atwix_custom_page> </layouts> </page>
After this, in the layout dropdown at the CMS editing page you can select your custom layout. See below:
Next, you should create your custom file atwix-page.phtml under app/design/frontend/../../template/page/atwix-page.phtml
You can just copy the content from 1column.phtml, 2columns-left.phtml, etc…
then you can insert the following snippet anywhere you want in your file (atwix-page.phtml)
<h1><?php echo Mage::getBlockSingleton('cms/page')->getPage()->getContentCustom(); ?></h1>
Function getContentCustom is returning a value for the ‘content_custom’ field from the ‘cms_page’ table
If you don’t want to create a template file, you can move the CMS page template file to the folder that contains your custom theme and the edit it.
UPDATE: I’ve found a different way to do this with layout xml. Magento allows to display your own CMS attribute on the frontend using another method. First, you should create the template file that will display your attribute values, in our case it is atwix/cmsattribute.phtml under app/design/frontend/base/default/template/
<h1><?php echo Mage::getBlockSingleton('cms/page')->getPage()->getContentCustom(); ?></h1>
function getContentCustom gets value of the ‘content_custom’ field from the ‘cms_page’ table.
After this, we need to add the following XML to your Layout Update XML field under the CMS -> Pages -> Your page -> Design tab.
<reference name="content"> <block type="core/template" name="home" template="atwix/cmsattribute.phtml"> </block> </reference>
In addition, it is important to note that attribute will be displayed after the page content, but if you want to display it before – you need to add the following XML:
<reference name="content"> <block type="core/template" name="home" template="atwix/cmsattribute.phtml" before="-"> </block> </reference>
‘before=”-“‘ this snippet gets Magento to display our block content before the general content.
If anyone has an alternative solution to a display custom CMS attribute on the frontend, please share your experience in the comments.