I'm searching for a way to get the main image from the first (or random) product from the current category and use it as a background for the category title.

As the title is created in 1column.phtml (or 2columns-left.phtml, 2columns-right.phtml, 3columns.phtml), the code should work there. I've tried all different solutions from this post, but it just gives me errors.

According to the code in 1column.phtml I've tried to get something like this as a first step inside :

<header class="page-header">
    <div class="container">...</div>


<?php if (Mage::app()->getFrontController()->getRequest()->getControllerName() == 'category') : ?>
<h1 class="title"><?php echo $_category->name ?></h1>
<?php echo $_category->getProductCollection()->getFirstItem()->getName(); ?>
<?php endif; ?>

This was an attempt that doesn't give back errors, but has no output at all. I'm pretty sure there is a was, but I'm clueless how to call anything useful within 1column.phtml.

  • Are you sure there is at least one product in the category that you have tested? And try to print $_category->getProductCollection()->getSelect() and paste the output query here May 5 '19 at 5:17

Please use registry variable for this case.

<?php if (Mage::app()->getFrontController()->getRequest()->getControllerName() == 'category') : 
    if ($_category = Mage::registry('current_category')) :
        $_helper    = Mage::helper('catalog/output');
        <h1 class="title"><?php echo $_helper->categoryAttribute($_category, $_category->getName(), 'name') ?></h1>
        <?php echo $_category->getProductCollection()->getFirstItem()->getName(); ?>
    <?php endif; ?>
<?php endif ;?>
  • Many thanks for the reply. However this gives me an error: Fatal error: Call to a member function categoryAttribute() on a non-object in /.../app/design/frontend/progressive/default/template/page/1column.phtml on line 172 If I add '<?php $_helper = $this->helper('catalog/output'); ?>' on top of 1column.phtml, the client side looks normal, but there is no output from 'echo $_category->getProductCollection()->getFirstItem()->getName();'
    – noojii
    Apr 2 '15 at 7:29
  • answer update ...
    – Amit Bera
    Apr 2 '15 at 8:03
  • Sorry, that doesn't give any output of 'echo $_category->getProductCollection()->getFirstItem()->getName();' aswell. Output is just: <header class="page-header"> <div class="container"> <h1 class="title">Title</h1> </div> </header>
    – noojii
    Apr 2 '15 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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