2

I have the following code in template/page/html/head.phtml which tries it best to supply facebook with an apropriate sharing image.(og:image)

Everything works, except for the part that I'm trying to get the content text of a cms/page. Logic dictates that if I use Mage::getSingleton('cms/page')->getContent() that I should get the HTML content of the cms page being viewed. However, 0 is returned to my function so I'm stuck with my default images on cms pages.

What am I doing wrong?

<?php
// viewing a product, get the image of the product for social sharing
if (Mage::registry('current_product'))
    {
    echo Mage::helper('catalog/image')->init(Mage::registry('current_product'), 'small_image')->resize(213);
    }
// this is where I get nothing returned from the content of the current page being viewed.
elseif($cms_content = Mage::getSingleton('cms/page')->getContent())
    {
    $src = array();
    $pos = 0;
    while(strpos($cms_content,'<img ',$pos)!== false)
        {
        // do a lot of complicated stuff here, not relevant for question
        }
    $theone = isset($src[0]) ? $src[0] : false;
    echo $theone;
    }
// show default image
else
    {
    echo 'default image url';
    }

1 Answer 1

7

You can get the current page like this:

$pageId = Mage::app()->getRequest()->getParam('page_id', Mage::app()->getRequest()->getParam('id', false));
$page = Mage::getModel('cms/page')->load($pageId);

To get the content use

$page->getContent();

but this will get you only the content in the database. So if you have directives like {{store}} or {{media}} they won't be parsed.
To get the parsed content use this:

$content = Mage::helper('cms')->getPageTemplateProcessor()->filter($page->getContent());
3
  • Thank you, your solution totally worked out. Now to tackle the aw_blog monster, do you happen to know how to get the current blog post from the aw_blog extension also? Commented Feb 21, 2014 at 14:58
  • 1
    @MichaelDibbets. I say this is a new different question. Let's keep things simple. Add a new question. Also you can contact the guys at AW. But I think you have a better change of getting an answer here.
    – Marius
    Commented Feb 21, 2014 at 14:59
  • I've followed up on your idea magento.stackexchange.com/questions/15527/… Thank you again for your help. Commented Feb 21, 2014 at 15:18

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.