How to get url paths in static blocks and phtml.

By on 12-21-2012 in 代码

Phtml:

//Multi Language Get Category name from ID
<?php echo Mage::getModel('catalog/category')->load(23)->getName();?>
//Multi Language Get Category url from ID
<?php echo Mage::getModel('catalog/category')->load(72)->getUrl();?>
//Multi Language Get Product url from ID
<?php echo Mage::getModel('catalog/product')->load(95)->getProductUrl();?>
//The second images
<?php $_productImage = Mage::getModel('catalog/product')->load($_product->getId());foreach ($_productImage->getMediaGalleryImages() as $image) { $_productGallery = $image->getUrl();break;};echo $_productGallery; ?>
//The second images thumbnail
<?php $_productImage = Mage::getModel('catalog/product')->load($_product->getId()); foreach ($_productImage->getMediaGalleryImages() as $image) { echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $image->getFile())->resize(246, 328);break;}?>
<?php $_productImage = Mage::getModel('catalog/product')->load($_product->getId());if(count($_productImage->getMediaGalleryImages())<2){echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->resize(246,null);}else{$_index = 1;foreach ($_productImage->getMediaGalleryImages() as $image) { if($_index==2){echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $image->getFile())->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->resize(246,null);break;}else{$_index++;}}}?>
//Not secure Skin URL
<?php echo $this->getSkinUrl('images/sampleimage.jpg') ?>
//Secure Skin URL
<?php echo $this->getSkinUrl('images/sampleimage.gif', array('_secure'=>true)) ?>
//Get  Current URL
$current_url = Mage::helper('core/url')->getCurrentUrl();
//Get Home URL
$home_url = Mage::helper('core/url')->getHomeUrl();
<?php echo Mage::getBaseUrl()?>/sample.jpg
//Get Magento Media URL
<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);?>
<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);?>
//Get Magento Skin URL
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);?>
//Get Magento Store URL
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
//Get Magento Js URL
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
//Get Block
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('header-time')->toHtml();?>
//replace magento's <p></p> & split data
<?php
   $header_time = $this->getLayout()->createBlock('cms/block')->setBlockId('header-time')->toHtml();
   $header_time = str_replace(array('<p>','</p>'), '', $header_time);
   $show_time = explode('|', $header_time);    
?>
$show_time[0]
$show_time[1]
//content by store
<?php $storeId = Mage::app()->getStore()->getId();
if($storeId == 1){?>
content
<?php } ?>
//content by category
<?php if(in_array(8,$_product->getCategoryIds()) || in_array(3,$_product->getCategoryIds()) || in_array(7,$_product->getCategoryIds())){?>
content
<?php } ?>
//image url by product
<?php $_product = Mage::registry('current_product'); ?>
<?php if(isset($_product)): ?>
<meta property="og:image" content="<?php echo $this->helper('catalog/image')->init($_product, 'image')->resize(500,500) ?>"/>
<?php else: ?>
<meta property="og:image" content=""/>
<?php endif; ?>

URL paths for static blocks:

<!-- To get SKIN URL -->
{{skin url='images/sampleimage.jpg'}}
<!-- To get Media URL -->
{{media url='/sampleimage.jpg'}}
<!-- To get Store URL -->
{{store url='mypage.html'}} {{config path="web/unsecure/base_url"}}
<!-- To get Base URL -->
{{base url='yourstore/mypage.html'}}

http://workbench.no/blog/magento-url-path/

Magento Mobile Page

		<select onchange="location.href=this.value;" name="un_jtt_pap_page_select">
			<?php if (!$this->isFirstPage()): ?>
				<option value="<?php echo $this->getFirstPageUrl() ?>"><?php echo $this->__('1')?></option>
        	<?php endif;?>
			<?php foreach ($this->getFramePages() as $_page): ?>
				<?php if ($this->isPageCurrent($_page)): ?>
					<option selected="selected"><?php echo $_page ?></option>
				<?php else: ?>
					<option value="<?php echo $this->getPageUrl($_page) ?>"><?php echo $_page ?></option>
				<?php endif;?>
			<?php endforeach;?>
			<?php if (!$this->isLastPage()): ?>
				<option value="<?php echo $this->getLastPageUrl() ?>"><?php echo $this->getLastPageNum() ?></option>
        	<?php endif;?>
		</select>
亲~ 想说点儿什么不?