Add an image every 3 posts in Wordpress/Magento PHP loop - php

I'm trying to add an image into my blog feed every three posts. I am using a wordpress integration extension for Magento by Fishpig.
This is the code I have to work with:
<?php $posts = $this->getPosts() ?>
<?php if (count($posts) > 0): ?>
<div class="post-list">
<ul id="post-list">
<?php foreach($posts as $post): ?>
<li class="item<?php if ($post->isSticky()): ?> featured is-sticky<?php endif; ?> <?php echo $post->getPostFormat() ?>">
<?php echo $this->getPostRenderer($post)->toHtml() ?>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList($('post-list'));</script>
<?php echo $this->getPagerHtml() ?>
</div>
<?php endif; ?>
Any help would be massively appreciated.
Thanks

Its simple.
Just use below code:
<?php $posts = $this->getPosts() ?>
<?php if (count($posts) > 0): ?>
<div class="post-list">
<ul id="post-list">
<?php $count=0; foreach($posts as $post): ?>
<li class="item<?php if ($post->isSticky()): ?> featured is-sticky<?php endif; ?> <?php echo $post->getPostFormat() ?>">
<?php $count++;if($count%3==0){?>
<img src="<?php echo {you image url}?>" />
<?php }?>
<?php echo $this->getPostRenderer($post)->toHtml() ?>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList($('post-list'));</script>
<?php echo $this->getPagerHtml() ?>
</div>
<?php endif; ?>

Related

splitting the paragraph into 2 sections in wordpress

I'm building a property section in WordPress that leaves me with a long and excruciating list of information when the code draws the data. This list should be divided into 2 sections to save more space on the page when on desktops, etc.
<a name="rooms"></a>
<h3><?php echo __("Property Details", "wppf"); ?></h3>
<?php foreach ($paragraphs as $paragraph) : ?>
<div class="clearfix">
<?php if ($paragraph['name'] != '') : ?>
<h4><?php echo $paragraph['name'] ?></h4>
<?php endif; ?>
<?php if ($paragraph['filesortorder'] != '') : ?>
<?php echo ($paragraph['filesortorder']); ?>
<?php endif; ?>
<p>
<?php if ($paragraph['dimensions'] != "") : ?>
<em><?php echo $paragraph['dimensions'] ?></em>
<br />
<?php endif; ?>
<?php echo $paragraph['description'] ?>
</p>
</div>
<?php endforeach; ?>
<?php foreach ($links as $link) : ?>
<div class="clearfix">
<a href="<?php echo $link['url']; ?>" target="_blank">
<?php echo (empty($link['name'])) ? $link['url'] : $link['name']; ?>
</a>
</div>
<?php endforeach; ?>
There is a maximum of 10 sections that it fills in and I want to split that into 2 groups of 5.
Something like...
if(count($paragraphs) > 5) {
$paragraphs2 = array_splice($paragraphs, 5);
}
Would let you split the paragraphs > 5 into another array.
You'd then just need to output those in your code as you see fit - something like:
<?php foreach ($paragraphs2 as $paragraph2) :?>
<div class="clearfix">
<?php if ($paragraph2['name']!=''):?>
<h4>
<?php echo $paragraph2['name'] ?>
</h4>
<?php endif; ?>
<?php if ($paragraph2['filesortorder']!=''):?>
<?php echo ($paragraph2['filesortorder']); ?>
<?php endif; ?>
<p>
<?php if ($paragraph2['dimensions']!=""):?>
<em>
<?php echo $paragraph2['dimensions'] ?>
</em>
<br />
<?php endif;?>
<?php echo $paragraph2['description'] ?>
</p>
</div>
<?php endforeach;?>
Should get you started to loop through and repeat the relevant display code.

How to show k2 items in k2 content module in 3 columns?

Joomla k2 component is power Joomla content system. k2 has a module that's called k2 content module and you can override the layout template of it.
I had this code for my k2 content module, this template file shows k2 items in 2 columns with even and odd classes and extra fields in it.
<?php
// no direct access
defined('_JEXEC') or die;
// includes placehold
$yt_temp = JFactory::getApplication()->getTemplate();
include (JPATH_BASE . '/templates/'.$yt_temp.'/includes/placehold.php');
?>
<div id="k2ModuleBox<?php echo $module->id; ?>" class="k2ItemsBlock<?php
if($params->get('moduleclass_sfx')) echo ' '.$params-
>get('moduleclass_sfx'); ?>">
<?php if($params->get('itemPreText')): ?>
<p class="modulePretext"><?php echo $params->get('itemPreText'); ?></p>
<?php endif; ?>
<?php if(count($items)): ?>
<ul class="product">
<?php foreach ($items as $key=>$item): ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; if(count($items)==$key+1)
echo ' lastItem'; ?>">
<?php if($params->get('itemImage') || $params->get('itemIntroText')): ?>
<?php if($params->get('itemImage')): ?>
<div class="product-image">
<img src="<?php echo $item->image; ?>" alt="<?php echo
K2HelperUtilities::cleanHtml($item->title); ?>" />
</div>
<?php endif; ?>
<div class="main">
<?php if($params->get('itemTitle')): ?>
<p class="moduleItemTitle"><?php echo $item->title; ?></p>
<?php endif; ?>
<?php if($params->get('itemIntroText')): ?>
<div class="introtext">
<?php echo $item->introtext; ?>
</div>
<?php endif; ?>
<!-- end main-->
<?php if($params->get('itemExtraFields') && count($item->extra_fields)): ?
>
<div class="moduleItemExtraFields-energi">
<ul>
<?php foreach ($item->extra_fields as $extraField): ?>
<?php if($extraField->value != ''): ?>
<li class="type<?php echo ucfirst($extraField->type); ?>
group2">
<?php if($extraField->type == 'header'): ?>
<h4 class="moduleItemExtraFieldsHeader"><?php echo
$extraField->name; ?></h4>
<?php else: ?>
<span class="moduleItemExtraFieldsLabel"><?php echo
$extraField->name; ?></span>
<span class="moduleItemExtraFieldsValue"><?php echo
$extraField->value; ?></span>
<?php endif; ?>
<div class="clr"></div>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
<div class="clr"></div>
</li>
<?php endforeach; ?>
<li class="clearList"></li>
</ul>
<?php endif; ?>
</div>
I would like to show my articles in 3 columns with extra fields.
How to do this? If it's possible, help me to do it.
Thanks for your attention.

Loop Through Magento Categories, Meta Keywords and Descriptions

I have a file that is successfully looping through my categories and subcategories. I am successfully able to echo all the categories and their links
BUT
I do not understand why these (keywords and description) are not echoing
<?php echo htmlspecialchars($this->getKeywords()) ?>
<?php echo htmlspecialchars($this->getDescription()) ?>
The file is located here
app/design/frontend/mystoretheme/default/template/catalog/category/listofcats.phtml
Then im placing it on a block in a cms page {{block type="catalog/navigation" name="catalog.category" template="catalog/category/listofcats.phtml"}}
The goal is to be able to display each of the categories list of keywords and their descriptions within the same < li > and loop giving me a list like this
Category
Keywords
Description
Here is my code. I have omitted my attempts at keyword and description since they are not working.
<div class="block block-list block-categories">
<div id="block-categories" class="block-title active">
<strong><span>Categories </span></strong>
</div>
<div id="leftnav" class="block-content" style="display:block">
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php if (count($categories) > 0): ?>
<ul id="leftnav-tree" class="level0">
<?php foreach($categories as $category): ?>
<li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">
<span><?php echo $this->escapeHtml($category->getName()) ?></span>
<?php //if ($this->isCategoryActive($category)): ?>
<?php $subcategories = $category->getChildren() ?>
<?php if (count($subcategories) > 0): ?>
<ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1">
<?php foreach($subcategories as $subcategory): ?>
<li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
<?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?>
<?php $secondLevelSubcategories = $subcategory->getChildren() ?>
<?php if (count($secondLevelSubcategories ) > 0): ?>
<ul id="leftnav-tree-<?php echo $subcategory->getId() ?>" class="level2">
<?php foreach($secondLevelSubcategories as $secondLevelSubcategory ): ?>
<li class="level2<?php if ($this->isCategoryActive($secondLevelSubcategory )): ?> active<?php endif; ?>">
<?php echo $this->escapeHtml(trim($secondLevelSubcategory ->getName(), '- ')) ?>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
<?php endif; ?>
<?php //endif; ?>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
<?php endif; ?>
</div>
Please check this:
<?php
$categories = Mage::getModel('catalog/category')
->getCollection()
->setStoreId(Put the store id here)
->addAttributeToSelect('*')
->addIsActiveFilter();
foreach ($categories as $category) {
echo $category->getName();
echo "-";
echo $category->getMetaKeywords();
echo "-";
echo $category->getMetaDescription();
echo "<br/>";
}
?>

Using getChildHtml to display product attributes not working

I have added a to my catalog.xml file, created a specifications.phtml file to be used as the template, and I’m calling echo $this->getChildHtml(’specifications’); in my view.phtml file.
However it’s not displaying anything at all. I’ve basically mirrored everything after the same type of thing for the product description (which works) but I’m not having any luck.
The content that I’m trying to pull in is under the product page and I think a custom section that is labeled “Specifications Tab”. Not sure if the space in the field name is causing issues or what.
Here is what I have in my view.phtml file:
<div id="product-tabs" class="tabs">
<ul class="tabs-nav">
<li><?php echo $this->__('Description'); ?></li>
<?php
$specificationsTabContent = $_product->getSpecificationsTab();
$videoTabContent = $_product->getVideoTab();
$faqTabContent = $_product->getFaqTab();
$howitworksTabContent = $_product->getHowitworksTab();
$awardsTabContent = $_product->getAwardsTab();
?>
<?php #if ($this->getChildHtml('specifications_tab')): #($this->getChildHtml('product_attributes')): ?>
<?php if (!empty($specificationsTabContent)) : ?>
<li><?php echo $this->__('Specs'); ?></li>
<?php endif; ?>
<?php if (!empty($videoTabContent)) : ?>
<li><?php echo $this->__('Videos14'); ?></li>
<?php endif; ?>
<?php if (!empty($faqTabContent)) : ?>
<li><?php echo $this->__('FAQs'); ?></li>
<?php endif; ?>
<?php if (!empty($howitworksTabContent)) : ?>
<li><?php echo $this->__('How It Works'); ?></li>
<?php endif; ?>
<?php if (!empty($awardsTabContent)) : ?>
<li><?php echo $this->__('Awards'); ?></li>
<?php endif; ?>
<!-- <li><?php #echo $this->__('Tags'); ?></li> -->
<?php if ($this->getChildHtml('snippet_product_view_tab_1')): ?>
<li><?php echo $this->__('Custom 1'); ?></li>
<?php endif; ?>
<?php if ($this->getChildHtml('snippet_product_view_tab_2')): ?>
<li><?php echo $this->__('Custom 2'); ?></li>
<?php endif; ?>
</ul>
<div class="tabs-content">
<div id="tab-description">
<?php echo $this->getChildHtml('description'); ?>
</div>
<div id="tab-details">
<?php #echo $this->getChildHtml('specifications'); ?>
<?php echo $specificationsTabContent; ?>
</div>
<div id="tab-faq">
<?php echo $faqTabContent; ?>
</div>
<div id="tab-video">
<?php echo $videoTabContent; ?>
</div>
<div id="tab-howitworks">
<?php echo $howitworksTabContent; ?>
</div>
<div id="tab-awards">
<?php echo $awardsTabContent; ?>
</div>
<!-- <div id="tab-tags">
<?php echo $this->getChildHtml('product_additional_data') ?>
</div> -->
<?php if ( $this->getChildHtml('snippet_product_view_tab_1') ): ?>
<div id="tab-custom-1">
<div class="snippet snippet-product-view-tab-1"><?php echo $this->getChildHtml('snippet_product_view_tab_1') ?></div>
</div>
<?php endif; ?>
<?php if ( $this->getChildHtml('snippet_product_view_tab_2') ): ?>
<div id="tab-custom-2">
<div class="snippet snippet-product-view-tab-2"><?php echo $this->getChildHtml('snippet_product_view_tab_2') ?></div>
</div>
<?php endif; ?>
</div>
</div>
This is the XML I added to my catalog.xml:
<!-- specifications -->
<block type="catalog/product_view_specifications" name="product.specifications.tab" as="specifications" template="catalog/product/view/specifications.phtml"/>
And this is the template file specifications.phtml:
<?php $_specifications = $this->getProduct()->getSpecificationsTab(); ?>
<?php if ($_specifications): ?>
<h2><?php echo $this->__('Specifications') ?></h2>
<div class="long-description std">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_specifications, 'specifications') ?>
</div>
<?php endif; ?>
Any help would be greatly appreciated! I’m not overly familiar with Magento or PHP.
Thanks,
JUst change
<?php #echo $this->getChildHtml('specifications'); ?>
to
<?php echo $this->getChildHtml('specifications'); ?>

Magento category link to 404 error

this is my first Magento project and I've run into such a big issue.
The Magento version I'm using is 1.7.0.2
I managed to create a custom left bar navigation bar using the following code in my "app/design/frontend/default/default/template/catalog/navigation/category.phtml"
<?php
$cats = Mage::getModel('catalog/category')->load(1)->getChildren();
$catIds = explode(',',$cats);
?>
<div id="LeftCategory">
<h2>Wicked Categories</h2>
<hr style="color:white; height:1px; margin:5px;" />
<ul>
<?php foreach($catIds as $catId): ?>
<li class="Li-Top-Category">
<?php
$category = Mage::getModel('catalog/category')->load($catId);
?>
<a href="<?php echo $category->getUrl()?>">
<?php echo $category->getName()?>
</a>
<?php
$subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren();
$subCatIds = explode(',',$subCats);
?>
<?php if(count($subCatIds) > 1):?>
<ul>
<?php foreach($subCatIds as $subCat) :?>
<li class="Li-Sub-Category">
<?php
$subCategory = Mage::getModel('catalog/category')->load($subCat);
?>
<a href="<?php echo $subCategory->getUrl()?>">
<?php echo $subCategory->getName()?>
</a>
<?php
?>
</li>
<?php endforeach;?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
It does the work to generate the category navigation. But the issue is, the URL each link points to doesn't work. All links are invalid that lead to the 404 Error Page.
You can see it here: http://wicked-shop.dev.thejinstudio.com/shop/catalog/category/view/s/accessories/id/15/
I've done so much search and nothing really solved this issue.
I appreciate your help in advance.
Use the catalog/category helper to get the proper URL from a category model
$_catalogCatgoryHelper = Mage::helper('catalog/category');
Then with that helper pass your category to the getCategoryUrl() function
$_catalogCatgoryHelper->getCategoryUrl($category);
So give this a try. I've put my suggestions into your code:
<?php
$_catalogCatgoryHelper = Mage::helper('catalog/category');
$cats = Mage::getModel('catalog/category')->load(1)->getChildren();
$catIds = explode(',',$cats);
?>
<div id="LeftCategory">
<h2>Wicked Categories</h2>
<hr style="color:white; height:1px; margin:5px;" />
<ul>
<?php foreach($catIds as $catId): ?>
<li class="Li-Top-Category">
<?php
$category = Mage::getModel('catalog/category')->load($catId);
?>
<a href="<?php echo $_catalogCatgoryHelper->getCategoryUrl($category) ?>">
<?php echo $category->getName()?>
</a>
<?php
$subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren();
$subCatIds = explode(',',$subCats);
?>
<?php if(count($subCatIds) > 1):?>
<ul>
<?php foreach($subCatIds as $subCat) :?>
<li class="Li-Sub-Category">
<?php
$subCategory = Mage::getModel('catalog/category')->load($subCat);
?>
<a href="<?php echo $_catalogCatgoryHelper->getCategoryUrl($subCategory)?>">
<?php echo $subCategory->getName()?>
</a>
<?php
?>
</li>
<?php endforeach;?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
You might be better off with this code which I used in the past (but actually ended up rewriting so that it iteratively displays categories regardless of how deep they go, looking into how to do this would certainly be a good exercise for you). It is a bit cleaner, but you'll need to modify it slightly to your needs:
<?php $helper = $this->helper('catalog/category') ?>
<div class="block block-categorynavigation">
<div class="block-title">
<strong><span><?php echo $this->__('Category') ?></span></strong>
</div>
<div class="block-content">
<?php $categories = $this->getStoreCategories() ?>
<?php if (count($categories) > 0): ?>
<ul id="leftnav-tree" class="level0">
<?php foreach($categories as $category): ?>
<li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">
<span><?php echo $this->escapeHtml($category->getName()) ?></span>
<?php $subcategories = $category->getChildren() ?>
<?php if (count($subcategories) > 0): ?>
<ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1">
<?php foreach($subcategories as $subcategory): ?>
<li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
<?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php if (count($subcategories) > 0): ?>
<ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level2">
<?php foreach($subsubcategories as $subsubcategory): ?>
<li class="level2<?php if ($this->isCategoryActive($subsubcategory)): ?> active<?php endif; ?>">
<?php echo $this->escapeHtml(trim($subsubcategory->getName(), '- ')) ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
edit: Original code only showed subcatgories for the currently viewed category

Categories