Search code examples
htmlmagento-1.8php

ad thumbnail to aw blog posts in magento


in magento 1.8.1 i have added a thumbnail image to aw blog posts header and into the aw blog sidebar widget next to the each headline

i did these:

In app/code/community/AW/Blog/Block/Manage/Blog/Edit/Form.php

change:

$form = new Varien_Data_Form(array(
    'id' => 'edit_form',
    'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
    'method' => 'post',
 ));

to

$form = new Varien_Data_Form(array(
    'id' => 'edit_form',
    'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
    'method' => 'post',
    'enctype' => 'multipart/form-data'
 ));

In app/code/community/AW/Blog/Block/Manage/Blog/Edit/Tab/Form.php

add

$fieldset->addField('featured_image', 'image', array(
    'name' => 'featured_image',
    'label' => 'Featured Image'
));

below the line

$fieldset = $form->addFieldset('blog_form', array('legend' => Mage::helper('blog')->__('Post information')));

In app/code/community/AW/Blog/controllers/Manage/BlogController.php

add

if(isset($_FILES['featured_image']['name']) and (file_exists($_FILES['featured_image']['tmp_name']))) {
   try {
      $uploader = new Varien_File_Uploader('featured_image');
      $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
      $uploader->setAllowRenameFiles(false);

      // setAllowRenameFiles(true) -> move your file in a folder the magento way
      // setAllowRenameFiles(true) -> move your file directly in the $path folder
      $uploader->setFilesDispersion(false);

      $path = Mage::getBaseDir('media') . DS .'blogpic'.DS;

      $uploader->save($path, $_FILES['featured_image']['name']);

      $data['featured_image'] = $_FILES['featured_image']['name'];
   }catch(Exception $e) {

   }
}

// handle delete image
else {
   if(isset($data['featured_image']['delete']) && $data['featured_image']['delete'] == 1)
      $data['image_main'] = '';
   else
      unset($data['featured_image']);
}

below the line

$model = Mage::getModel('blog/post');

And added a "featured_image" column to the aw_blog table in database. Use the type VARCHAR(255) default null and null enable .

and add

<img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).$post->getFeaturedImage() ?>" alt="featuredImage" />

to

/app/design/frontend/base/default/template/aw_blog/blog.phtml

below line

<?php foreach ($posts as $post): ?>
    <div class="postWrapper">
        <div class="postTitle">
            <h2><a href="<?php echo $post->getAddress(); ?>" ><?php echo $post->getTitle(); ?></a></h2>

the problem is: the images are saving in right place in media/blogpic but they are not shown in frontend


Solution

  • In your blog.phtml subst

    <img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).$post->getFeaturedImage() ?>" alt="featuredImage" />
    

    with

    <img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).DS.'blogpic'.DS.$post->getFeaturedImage() ?>" alt="featuredImage" />
    

    Hope it helps.