Wordpress: show featured image with subcategory - image

I'm using this to generate a list of sub-pages that are a parent of 10 and images:
<ul>
<?php wp_list_pages('title_li=&child_of=10&link_after=<img src="http://mydomain.com/image.gif" alt="" />'); ?>
</ul>
It works, but the problem is I don't know how to get the post featured image there instead. I tried this but it did not work:
<?php wp_list_pages('title_li=&child_of=10&link_after=<img src="' . the_post_thumbnail(array(100,50)) . '" alt="" />'); ?>
Obviously I'm missing something.
Any suggestions would be appreciated.

To display the page titles along with the images, you should use get_pages()
<?php
$pages = get_pages('child_of=10');
if ($pages) {
echo '<ul>';
foreach ($pages as $page) {
echo '<li><a href="'.get_permalink($page->ID).'">';
echo get_the_title($page->ID);
echo get_the_post_thumbnail($page->ID);
echo '</a></li>';
}
echo '</ul>';
}
?>

The get_the_post_thumbnail function returns HTML not the image url.
Instead use
<?php $image_url = wp_get_attachment_image_src( get_post_thumbnail_id(10), array(100,50) ); ?>
<?php wp_list_pages('title_li=&child_of=10&link_after=<img src="' . $image_url . '" alt="" />'); ?>
This will list all sub-pages of page id 10, with thumbnail of page id 10. If you want the thumbnails of the sub-pages instead of parent page, you will have to write custom code instead of wp_list_pages function(as explained by Indranil).

Related

how to give link to each attributes of a manufacturer

I have added 4 to 5 attributes in manufacture and shown in right column of front end.Now i want to link the each attributes of manufacture. if i will click an attribute of a manufacturer, it will show all the products which contain that arribute/brand.
if anyone knows this, please help me out.
thanks!
I have shown the attribute name in front end by the below code
<?php
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'Manufacturer');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
if(count( $options)>0){
?>
<div class="title_box">Manufacturers</div>
<?php $i=1;?>
<ul class="left_menu">
<?php
foreach($options as $eachval){
?>
<?php if($i%2==0){ ?>
<li class="even"><?php echo $eachval['label']?></li>
<?php } else { ?>
<li class="odd"><?php echo $eachval['label']?></li>
<?php } $i++; ?>
<?php } ?>
</ul>
<?php } } ?>
I have made one page manu.phtml in catalog/product page and put the following above code now how to give link to that arribute...........please describe briefly
in href link,what i have to write so that when i will click on any attribute it will show all products associated to that attribute/brand.
There is always the option of creating a new module with a custom controller that would list the products from a specified brand, but that is a painful process even if it's the clean way.
Here is a simple version if you don't mind the ugly urls.
The main idea is to link your brand names to the advanced search page with a specific brand filled in.
You can get the url like this:
$url = Mage::getUrl('catalogsearch/advanced/result', array('_query'=>'brand='.$value->getId()))
You just need now to get the id of the specific brand ($value->getId()), but if you can get the name you can get the id also.
And don't forget to specify that the brand attribute is used in advanced search. You can do that by editing the attribute in the backend.
[EDIT]
Make your ul element look like this:
<ul class="left_menu">
<?php
foreach($options as $eachval){
$url = Mage::getUrl('catalogsearch/advanced/result', array('_query'=>'Manufacturer='.$eachval['value']));
?>
<?php if($i%2==0){ ?>
<li class="even"><?php echo $eachval['label']?></li>
<?php } else { ?>
<li class="odd"><?php echo $eachval['label']?></li>
<?php } $i++; ?>
<?php } ?>
</ul>
Small tip off topic. You can avoid duplication of the li elements in your code like this
<ul class="left_menu">
<?php
foreach($options as $eachval){
$url = Mage::getUrl('catalogsearch/advanced/result', array('_query'=>'Manufacturer='.$eachval['value']));
?>
<li class="<?php echo ($i%2 == 0) ? 'even':'odd';?>"><?php echo $eachval['label']?></li>
<?php } $i++; ?>
<?php } ?>
</ul>

Update woocommerce shopping cart icon dynamically with ajax

I Have searched for an answer to this, but not been able to find one.
In my woocommerce shop, i want a basket icon at the top of the site. When a customer puts something in the cart, they want to swap this icon for a different one - this icon ("not empty") will stay as long as the cart is not empty.
What I'm after is a PHP-type if / else call, which says "if the cart is empty, display icon A, else display icon B". If anyone could shine some light on this, I'd be most grateful!
I am able to update the cart text dynamically following this woocommerce tutorial,
http://docs.woothemes.com/document/show-cart-contents-total/
In my Page
<?php global $woocommerce;
if($woocommerce->cart->get_cart_total()=='0') { ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/empty_cart.png" alt="" width="30" height="30">
<?php }else{ ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/full_cart.png" alt="" width="30" height="30">
<?php } ?>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" >Your Cart : <?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
In Functions.php
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
if($woocommerce->cart->get_cart_total()=='0') { ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/empty_cart.png" alt="" width="30" height="30">
<?php
}else{ ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/full_cart.png" alt="" width="30" height="30">
<?php }
?>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" >Your Cart : <?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
currently it is showing both the images no matter what is in the cart.
You can see the implementation of the above at
http://fungass.com/testing/shop/uncategorized/abc/
Finally solved it like this,
<?php global $woocommerce; ?>
<a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" ><?php if ($woocommerce->cart->cart_contents_count == 0){
printf( '<img src="%s/images/empty.png" alt="empty" height="25" width="25">', get_stylesheet_directory_uri());
}else{
printf( '<img src="%s/images/full.png" alt="full" height="25" width="25">', get_stylesheet_directory_uri());
}
?> </a>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" >
Your Cart : <?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
I would actually recommend that you use cookies & JavaScript to handle this functionality for the added benefit of allowing caching plugins to serve static pages on non-WooCommerce pages but still have some dynamic pieces on the page (like cart totals or just simply a populated cart icon).
On the PHP side of things, on init, check for cart totals and set a cookie value...
// this should only ever fire on non-cached pages (so it SHOULD fire
// whenever we add to cart / update cart / etc
function update_cart_total_cookie()
{
global $woocommerce;
$cart_total = $woocommerce->cart->cart_contents_count;
setcookie('woocommerce_cart_total', $cart_total, 0, '/');
}
add_action('init', 'update_cart_total_cookie');
On the JavaScript side of things, examine the cookie and then change the icon...
// use the custom woocommerce cookie to determine if the empty cart icon should show in the header or the full cart icon should show
// *NOTE: I'm using the jQuery cookie plugin for convenience https://github.com/carhartl/jquery-cookie
var cartCount = $.cookie("woocommerce_cart_total");
if (typeof(cartCount) !== "undefined" && parseInt(cartCount, 10) > 0) {
$(".full-cart-icon").show();
$(".empty-cart-icon").hide();
// optionally you can even use the cart total count if you want to show "(#)" after the icon
// $(".either-cart-icon span").html("(" + cartCount + ")");
}
else {
$(".full-cart-icon").hide();
$(".empty-cart-icon").show();
}
Hope this helps a bit! Have fun!

Displaying image from RSS

I have this feed item via SimplePie:
<item>
<guid isPermaLink="false">tncms-asset-7f55e8da-c801-11e2-9a4d-001a4bcf887a</guid>
<title>Parts of old span may be used in bridge repair</title>
<author>Author Person</author>
<link>http://www.mysite.com/a.html</link>
<description><![CDATA[Description Here.]]></description>
<pubDate>Tue, 28 May 2013 18:45:00 -0700</pubDate>
<enclosure url="http://mysite.com/happyLilPic.jpg" length="512" type="image/jpeg" />
</item>
I can get everything else to show except the image with the following:
<?php
foreach ($feed->get_items() as $item):
?>
<div class="item">
<h3><?php echo $item->get_title(); ?></h3>
<p><?php echo $item->get_description(); ?></p>
<?php echo "<img src=\"" . $item->get_enclosure['url'][0] . "\">"; ?>
<br><br>
</div>
<?php endforeach; ?>
What am I doing wrong with the enclosure???
get_enclosure() returns a single array location containing a SimplePie_Enclosure object. The documentation shows it this way:
if ($enclosure = $item->get_enclosure())
{
echo $enclosure->embed();
}
That uses the <embed> tag though, which is not what you want. The documentation for the Enclosure Object Lists many methods. The one I think you want is get_link(), so you would change your image tag code like this:
if ($enclosure = $item->get_enclosure())
{
echo "<img src=\"" . $enclosure->get_link() . "\">";
}

Hiding Magento's Layered Navigation from Search Engines

I’m hoping someone can help me with this problem I’ve been trying to solve for the past few days. I want to hide Magento’s Layered Navigation from the search engines entirely, but make it available to users. For SEO reasons, I don’t want to settle for NoFollowing all the links, or using noindex follow meta tags, or even blocking it entirely with Robots.txt. The most effective way of handling this would be only showing the layered Navigation to users with Cookies enabled, since Google doesn’t use cookies. The same effect could probably be achieved with JavaScript as well, but I’ve chosen the Cookie method.
So far I’ve managed to implement a crude piece of JS to check if cookies are enabled once the page has loaded (adapted from another thread on this forum). If cookies are enabled, it does nothing and layered nav displays, but if cookies are not enabled, I want to remove the “catalog.leftnav” block. I can’t for the life of me figure out how to do this from my JS script. All I’ve been able to achieve is removing the div element, or setting style.display to none etc., and while all of these techniques remove the links from the frontend, Google can still see them all. Here’s an example of the code I have so far in template/catalog/layer/filter.phtml
<div id="shop-by-filters">
<ol>
<?php foreach ($this->getItems() as $_item): ?>
<li>
<?php if ($_item->getCount() > 0): ?>
<?php echo $_item->getLabel() ?>
<?php else: echo $_item->getLabel() ?>
<?php endif; ?>
<?php if ($this->shouldDisplayProductCount()): ?>
(<?php echo $_item->getCount() ?>)
<?php endif; ?>
</li>
<?php endforeach ?>
</ol>
</div>
<script type="text/javascript">
if (navigator.cookieEnabled) {
return true;
} else if (navigator.cookieEnabled === undefined) {
document.cookie = "testcookie";
if (cookie_present("testcookie"))
return true;
} else {
var elem = document.getElementById('shop-by-filters');
elem.parentNode.removeChild(elem);
}
</script>
Can anyone help me with this, or is there a better way of going about it? Please keep in mind that I am still trying to get my head around Magento, so I might need some instructions if the implementation is complicated.
Thank you.
Brendon
I'm not sure if the Google robot will reliably parse your javascript.
You may be better off hiding the layered nav based on the current session with php.
<?php if (Mage::getSingleton('customer/session')): ?>
...your nav code...
<?php endif ?>
First of all, Javascript will do nothing to stop Google from indexing that content.
Why don't you want 'to settle for NoFollowing all the links'? That is exactly what NoFollow is for. You can also tell Google to not pay attention to the qualifiers/query strings in Webmaster Tools.
If for some reason you really wanted to hide that block from Google, edit the template and string compare $_SERVER['HTTP_USER_AGENT'] against Google's very public list of user agents here http://support.google.com/webmasters/bin/answer.py?hl=en&answer=1061943
EDIT -- string compare
<?php if (stripos($_SERVER['HTTP_USER_AGENT'], 'Googlebot') !== false): ?>
<div id="shop-by-filters">
<ol>
<?php foreach ($this->getItems() as $_item): ?>
<li>
<?php if ($_item->getCount() > 0): ?>
<?php echo $_item->getLabel() ?>
<?php else: echo $_item->getLabel() ?>
<?php endif; ?>
<?php if ($this->shouldDisplayProductCount()): ?>
(<?php echo $_item->getCount() ?>)
<?php endif; ?>
</li>
<?php endforeach ?>
</ol>
</div>
<?php endif; ?>
It is a slick subject. We used this code to hide the layered navigation from Google, but we are not sure is it working...
<div id="filters-no-follow"></div>
<?php
function prepare_for_echo($string) {
$no_br = trim(preg_replace('/\s+/', ' ', $string));
$no_slashes = str_replace('\'', '\\\'', $no_br);
return $no_slashes;
}
?>
<script>
function please_enable_cookies() {
var f = document.getElementById('filters-no-follow');
f.innerHTML = '<div class="no-cookies-error">Enable cookies to choose filters.</div>';
}
function please_load_filters() {
var f = document.getElementById('filters-no-follow');
f.innerHTML = '<?php if ( !empty($filtersHtml) || !empty($stateHtml) ): ?>'
+ '\n<div class="block block-layered-nav">'
+ '\n <div class="block-title">'
+ '\n <strong><span><?php echo prepare_for_echo($this->__('Shop By')); ?></span></strong>'
+ '\n </div>'
+ '\n <div class="block-content">'
+ '\n <?php echo prepare_for_echo($this->getStateHtml()); ?>'
+ '\n <?php if ($this->canShowOptions()): ?>'
+ '\n <p class="block-subtitle"><?php echo prepare_for_echo($this->__('Shopping Options')); ?></p>'
+ '\n <dl id="narrow-by-list">'
+ '\n <?php echo prepare_for_echo($filtersHtml); ?>'
+ '\n </dl>'
+ '\n <?php endif; ?>'
+ '\n </div>'
+ '\n</div>'
+ '\n<?php endif; ?>';
}
function are_cookies_enabled()
{
var cookieEnabled = (navigator.cookieEnabled) ? true : false;
if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
{
document.cookie="testcookie";
cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
}
return (cookieEnabled);
}
if(are_cookies_enabled()) {
please_load_filters();
} else {
please_enable_cookies();
}
</script>

Magento Front Page category display

I am trying to customize a Blanco Magento Theme I bought in Themeforest,
I want to display some categories (not all) in the front page with an static image, does anyone have an idea how to do it?
Thanks a lot!
PS: Magento is 1.7
This can be easily done if you need to display products on home page or any CMS page then do the following:
a) Create a new category of the products which you want to display on the desired page and note down the category Id generated (suppose id = 4)
b) Go to Admin->CMS->Pages and select the page you want
For adding any image add an img tag with the below src
src="{{media url="image_path"}}"
{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" category_id="4" template="catalog/product/list.phtml"}}
that's it
Hope this helps!!
You can put the following Code into a PHTML file and include it in your Front Page using XML or other way you want. As far as static image is concern, you can add Category image from Admin which will be display from the given code -
CODE is :
<?php
# CategoryIDs to be display on Front Page...
$_viewCategories = array(3, 4, 5);
# Categories Counter...
$counter = count($_viewCategories);
# Get Category Model
$getCategoryModel = Mage::getModel('catalog/category');
?>
<div class="categoryList">
<?php
for($i = 0; $i < $counter; $i++)
{
// Load Categories...
$getCategoryModel->load($_viewCategories[$i]);
$getParentCategoryName = $getCategoryModel->getName();
# echo "Parent Category Name : ".$getParentCategoryName;
?>
<div class="categoryListContainer">
<div class="categoryListHeading"><?php echo ucwords(strtolower($getParentCategoryName)); ?></div>
<?php
$getChildren = $getCategoryModel->getChildren();
$subCategories = explode(",", $getChildren);
$j = 0;
foreach ($subCategories as $_child) {
if($j >= 2){ break; }
?>
<div style="float:left; width:205px; <?if($j == 1):?>margin-left:25px;<?php endif; ?>" align="center">
<?php
$subCategoryDetail = Mage::getModel("catalog/category")->load($_child);
echo "<a href='".$subCategoryDetail->getUrl()."' title='".$subCategoryDetail->getName()."'>";
echo "<div class='categoryListImg'><img src='".$subCategoryDetail->getImageUrl()."' height='120px' width='120px' alt='".$subCategoryDetail->getName()."' /></div>";
echo "<div class='categoryListCaption'>";
echo "<span class='catName'><span>".$subCategoryDetail->getName()."</span></span>";
echo "</div>";
echo "</a>";
?></div>
<?php
$j++;
}
?>
</div>
<?php
}
?>
</div>
It's Style you can manage with yourself with your own Style :)
Hope it would be helpful for you!
Thanks :)

Resources