Magento WordPress Integration Post Images in Magento WordPress Integration

WordPress allows you to add an image to a post or page and set this image as the 'featured image'. This image is then automatically resized into different sizes that can be modified in the WordPress Admin. A thumbnail of this image will display automatically against the post in your integrated blog, however it is possible to display a featured image where ever you have a Post model.

The following code will display a post's featured image as a link to the post page.

<?php // $post is already defined ?>
<?php if ($featuredImage = $post->getFeaturedImage()): ?>
	<a href="<?php echo $post->getPermalink() ?>">
		<img src="<?php echo $featuredImage->getAvailableImage() ?>" alt="<?php echo $this->escapeHtml($post->getPostTitle()) ?>"/>
<?php endif; ?>

Firstly the code retrieves the featured image model by calling $post->getFeaturedImage() which returns an object of Fishpig_Wordpress_Model_Image. The code then calls $featuredImage->getAvailableImage() which returns the smallest version of the image it can find.

It is possible to get a specific sized image rather than just the first available image. The follow methods are available:


	$image = $post->getFeaturedImage();
	// Get the URL of the thumbnail image
	echo $image->getThumbnailImage();
	// Get the URL of the medium sized image
	echo $image->getMediumImage();
	// Get the URL of the large image
	echo $image->getLargeImage();
	// Get the URL of the full size image (this will be the original uploaded image size)
	echo $image->getFullSizeImage();
	// Get the post thumbnail image URL
	echo $image->getPostThumbnailImage();
	// Work through these images and get the first available image
	echo $image->getAvailableImage();

Post your comment

FishPig Ltd