Select Page

Show Related Posts Without A Plugin

Add the following code to your single.php.

 <?php
$tags = wp_get_post_tags($post->ID);
 if ($tags) {
     $tag_ids = array();
     foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
     $args=array(
     'tag__in' => $tag_ids,
     'post__not_in' => array($post->ID),
     'showposts'=>5, // Number of related posts that will be shown.
     'caller_get_posts'=>1
     );
 $my_query = new wp_query($args);
     if( $my_query->have_posts() ) {
         echo '<h3>Related Posts</h3><ul>';
         while ($my_query->have_posts()) {
             $my_query->the_post();
             ?>
             <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
         <?php
         }
         echo '</ul>';
     }
 }
 ?>

Adding your RSS Feed to Your Customers WordPress Dashboard

This can be used to add your company RSS feed to your customers WordPress dashboard. Simply add this code to the functions.php file.

<?php
function shaken_rss_output(){
echo '<div class="rss-widget">';
wp_widget_rss_output(array(
'url' => 'https://www.nick-shaw.co.uk/feed',  //put your feed URL here
'title' => 'Latest News', // Your feed title
'items' => 3, //how many posts to show
'show_summary' => 1, // 0 = false and 1 = true
'show_author' => 1,
'show_date' => 1
));

echo "</div>";
}

// Hook into wp_dashboard_setup and add our widget
add_action('wp_dashboard_setup', 'shaken_rss_widget');

// Create the function that adds the widget
function shaken_rss_widget(){
// Add our RSS widget
wp_add_dashboard_widget( 'shaken-rss', 'RSS Widget', 'shaken_rss_output');
}
?>

Automatically Create Meta Description From Your Posts Content

Add this to your functions.php file and it will automatically create a meta description from your WordPress post.

<?php
function create_meta_desc() {
    global $post;
    if (!is_single()) { return; }
    $meta = strip_tags($post->post_content);
    $meta = strip_shortcodes($post->post_content);
    $meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
    $meta = substr($meta, 0, 125);
    echo "<meta name='description' content='$meta' />";
}
add_action('wp_head', 'create_meta_desc');
?>

Automatically show child pages and custom fields WorldPress

This code will allow you to automatically show child pages including custom fields in this case one called ‘desc’

You will need to add this code to your template file for example page.php


  <?php
        $child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT');    ?>
 		<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
  			<div class="child-thumb" onclick="location.href='<?php echo $pageChild->post_title; ?>';" style="cursor: pointer;">
			<?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?>
            <a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>">
			<?php echo $pageChild->post_title; ?></a>
		    <?php
                $customFields = get_post_custom($pageChild->ID);
                echo '<p class="desc">' . $customFields['desc'][0] . '</p>';
            ?>
            </div>
         <?php endforeach; endif;
         ?>

Magento weight vs destination table rate shipping costs example

Here is an example of weight vs destination pricing for UK:

Country,Region/State,&quot;Zip/Postal Code&quot;,&quot;Weight (and above)&quot;,&quot;Shipping Price&quot;
GBR,*,,1000.0000,75.0000
GBR,*,,65.0000,50.5000
GBR,*,,50.0000,40.0000
GBR,*,,40.0000,30.0000
GBR,*,,30.0000,20.0000
GBR,*,,20.0000,17.0000
GBR,*,,10.0000,9.7500

This example has different weight based shipping costs for the UK.

Download Magento UK Tablerates

How to add custom attributes to transactional emails in Magento 1.5

Follow this example to add custom attributes to transactional emails in Magento 1.5 (may work in older version as well)

Create a new folder called templates in the directory app\design\frontend\default\default\ and create a new file called orderemail.phtml. Final URL should be app\design\frontend\default\default\template\orderemail.phtml

Add the following code to the file orderemail.phtml:

<?php
$order_id = $this->getData('order')->getRealOrderId();
$_order = $this->getOrder() ?>
<table cellspacing="0" cellpadding="0" border="0" width="100%" style="border:1px solid #bebcb7; background:#f8f7f5;">
    <thead>
        <tr>
            <th class="a-left" bgcolor="#d9e5ee" style="padding:3px 9px">Item</th>

            <th class="a-center" bgcolor="#d9e5ee" style="padding:3px 9px">Qty</th>
            <th class="a-right" bgcolor="#d9e5ee" style="padding:3px 9px">Subtotal</th>
        </tr>
    </thead>

    <tbody>
<?php $i=0; foreach ($_order->getAllItems() as $_item): $i++ ?>
        <tr <?php echo $i%2?'bgcolor="#eeeded"':'' ?>>
            <td align="left" valign="top" style="padding:3px 9px"><?php echo $_item->getName() ?>
            (            <?php
                /* Displays the product's custom attribute */
                $p = Mage::getModel('catalog/product');
                $p ->load($_item->getProductId());

                echo htmlentities($p->getArtistsurname()) ." ";
                echo htmlentities($p->getArtistchristianname());
            ?>
)
            </td>

            <td align="center" valign="top" style="padding:3px 9px"><?php echo sprintf('%s', $_item->getQtyOrdered()) ?></td>
            <td align="right" valign="top" style="padding:3px 9px">
            <?php if ($this->helper('tax')->displayCartPriceExclTax() || $this->helper('tax')->displayCartBothPrices()): ?>
                <?php if ($this->helper('tax')->displayCartBothPrices()): ?>
                    <span class="label"><?php echo $this->__('Excl. Tax'); ?>:</span>
                <?php endif; ?>
                <?php if (Mage::helper('weee')->typeOfDisplay($_item, array(0, 1, 4), 'email', $_order->getStore())): ?>
                    <?php echo $_order->formatPrice($_item->getRowTotal()+$_item->getWeeeTaxAppliedRowAmount()+$_item->getWeeeTaxRowDisposition()); ?>
                <?php else: ?>
                    <?php echo $_order->formatPrice($_item->getRowTotal()) ?>
                <?php endif; ?>

                <?php if (Mage::helper('weee')->getApplied($_item)): ?>
                    <br />
                    <?php if (Mage::helper('weee')->typeOfDisplay($_item, 1, 'email', $_order->getStore())): ?>
                        <small>
                        <?php foreach (Mage::helper('weee')->getApplied($_item) as $tax): ?>
                            <span class="nobr"><?php echo $tax['title']; ?>: <?php echo $_order->formatPrice($tax['row_amount'],true,true); ?></span><br />
                        <?php endforeach; ?>
                        </small>
                    <?php elseif (Mage::helper('weee')->typeOfDisplay($_item, 2, 'email', $_order->getStore())): ?>
                        <?php foreach (Mage::helper('weee')->getApplied($_item) as $tax): ?>
                            <span class="nobr"><small><?php echo $tax['title']; ?>: <?php echo $_order->formatPrice($tax['row_amount_incl_tax'],true,true); ?></small></span><br />
                        <?php endforeach; ?>
                    <?php elseif (Mage::helper('weee')->typeOfDisplay($_item, 4, 'email', $_order->getStore())): ?>
                        <small>
                        <?php foreach (Mage::helper('weee')->getApplied($_item) as $tax): ?>
                            <span class="nobr"><?php echo $tax['title']; ?>: <?php echo $_order->formatPrice($tax['row_amount_incl_tax'],true,true); ?></span><br />
                        <?php endforeach; ?>
                        </small>
                    <?php endif; ?>

                    <?php if (Mage::helper('weee')->typeOfDisplay($_item, 2, 'email', $_order->getStore())): ?>
                        <br />
                        <span class="nobr"><?php echo Mage::helper('weee')->__('Total'); ?>:<br /> <?php echo $_order->formatPrice($_item->getCalculationPrice()+$_item->getWeeeTaxAppliedRowAmount()+$_item->getWeeeTaxRowDisposition()); ?></span>
                    <?php endif; ?>
                <?php endif; ?>
            <?php endif; ?>

            <?php if ($this->helper('tax')->displayCartPriceInclTax() || $this->helper('tax')->displayCartBothPrices()): ?>
                <?php if ($this->helper('tax')->displayCartBothPrices()): ?>
                    <br /><span class="label"><?php echo $this->__('Incl. Tax'); ?>:</span>
                <?php endif; ?>
                <?php $_incl = $this->helper('checkout')->getSubtotalInclTax($_item); ?>
                    <?php if (Mage::helper('weee')->typeOfDisplay($_item, array(0, 1, 4), 'email', $_order->getStore())): ?>
                        <?php echo $_order->formatPrice($_incl+$_item->getWeeeTaxAppliedRowAmount()); ?>
                    <?php else: ?>
                        <?php echo $_order->formatPrice($_incl-$_item->getWeeeTaxRowDisposition()) ?>
                    <?php endif; ?>
                <?php if (Mage::helper('weee')->getApplied($_item)): ?>
                    <br />
                    <?php if (Mage::helper('weee')->typeOfDisplay($_item, 1, 'email', $_order->getStore())): ?>
                        <small>
                        <?php foreach (Mage::helper('weee')->getApplied($_item) as $tax): ?>
                            <span class="nobr"><?php echo $tax['title']; ?>: <?php echo $_order->formatPrice($tax['row_amount'],true,true); ?></span><br />
                        <?php endforeach; ?>
                        </small>
                    <?php elseif (Mage::helper('weee')->typeOfDisplay($_item, 2, 'email', $_order->getStore())): ?>
                        <?php foreach (Mage::helper('weee')->getApplied($_item) as $tax): ?>
                            <span class="nobr"><small><?php echo $tax['title']; ?>: <?php echo $_order->formatPrice($tax['row_amount_incl_tax'],true,true); ?></small></span><br />
                        <?php endforeach; ?>
                    <?php elseif (Mage::helper('weee')->typeOfDisplay($_item, 4, 'email', $_order->getStore())): ?>
                        <small>
                        <?php foreach (Mage::helper('weee')->getApplied($_item) as $tax): ?>
                            <span class="nobr"><?php echo $tax['title']; ?>: <?php echo $_order->formatPrice($tax['row_amount_incl_tax'],true,true); ?></span><br />
                        <?php endforeach; ?>
                        </small>
                    <?php endif; ?>

                    <?php if (Mage::helper('weee')->typeOfDisplay($_item, 2, 'email', $_order->getStore())): ?>
                        <span class="nobr"><?php echo Mage::helper('weee')->__('Total incl. tax'); ?>:<br /> <?php echo $_order->formatPrice($_incl+$_item->getWeeeTaxAppliedRowAmount()); ?></span>
                    <?php endif; ?>
                <?php endif; ?>
            <?php endif; ?>
            </td>
        </tr>
<?php endforeach ?>
    </tbody>

    <tfoot>
        <tr>
            <td colspan="2" align="right" style="padding:3px 9px"><?php echo Mage::helper('sales')->__('Subtotal') ?></td>
            <td align="right" style="padding:3px 9px"><?php echo $_order->formatPrice($_order->getSubtotal()) ?></td>
        </tr>
        <?php if ($_order->getDiscountAmount()): ?>
            <tr>
                <td colspan="2" align="right" style="padding:3px 9px"><?php echo Mage::helper('sales')->__('Discount') ?></td>
                <td align="right" style="padding:3px 9px"><?php echo $_order->formatPrice($_order->getDiscountAmount()) ?></td>
            </tr>
        <?php endif; ?>
        <?php if ($_order->getShippingAmount()): ?>
            <tr>
                <td colspan="2" align="right" style="padding:3px 9px"><?php echo Mage::helper('sales')->__('Shipping Amount') ?></td>
                <td align="right" style="padding:3px 9px"><?php echo $_order->formatPrice($_order->getShippingAmount()) ?></td>
            </tr>
        <?php endif; ?>
        <?php if ($_order->getTaxAmount()): ?>
            <tr>
                <td colspan="2" align="right" style="padding:3px 9px"><?php echo Mage::helper('sales')->__('Tax Amount') ?></td>
                <td align="right" style="padding:3px 9px"><?php echo $_order->formatPrice($_order->getTaxAmount()) ?></td>
            </tr>
        <?php endif; ?>
        <tr bgcolor="#DEE5E8">
            <td colspan="2" align="right" style="padding:3px 9px"><strong><big><?php echo Mage::helper('sales')->__('Grand Total') ?></big></strong></td>
            <td align="right" style="padding:6px 9px"><strong><big><?php echo $_order->formatPrice($_order->getGrandTotal()) ?></big></strong></td>
        </tr>
    </tfoot>

</table>

If you haven’t already create a new Transactional Email by going to System > Transactional Emails and click ‘Add New Template’, select the template  ‘New Order’ and click ‘Load Template’ (You may also want do do the same to other emails such as ‘New Shipment’), lastly give it a sensible name.

Replace the code (near the bottom):

{{layout handle="sales_email_order_items" order=$order}}

with the following code:

{{block type='core/template' area='frontend' template='orderemail.phtml' order=$order}}

Lastly you need to go to System > Configuration > Sales Emails and in the order section set the New Order Confirmation Template to you new template you created.

Thats all it should now be working.

mod_rewrites and SEO friendly Magento URLs

To enable mod_rewrites and SEO friendly Magento URLs which removes the index.php from the URL of your Magento store, you should enable mod_rewrites.

Step 1
Find this in the .htaccess file in the root of your website.

## you can put here your magento root folder

## path relative to web root

#RewriteBase /magento/

Step 2
Remove the # and magento/ so that it looks like:

## you can put here your magento root folder

## path relative to web root

RewriteBase /

Step 3
Go to Magento admin pannel: System > Configuration > Web > Search Engines Optimization > Use Web Server Rewrites:and set to: YES

You should now be able to see your pages without the index.php in the URL.

Display a Random Header Image on Your WordPress Blog

Upload some images named 1.jpg, 2.jpg, 3.jpg and so on to your your wp-content/themes/yourtheme/images directory.

Open header.php and paste the following code in it:

<?php
$num = rand(1,10); //Get a random number between 1 and 10, assuming 10 is the total number of header images you have
?>

Remove RSS feeds from WordPress site

Add this to your functions.php file to disable any RSS feeds.

function fb_disable_feed() {
wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') );
}

add_action('do_feed', 'fb_disable_feed', 1);
add_action('do_feed_rdf', 'fb_disable_feed', 1);
add_action('do_feed_rss', 'fb_disable_feed', 1);
add_action('do_feed_rss2', 'fb_disable_feed', 1);
add_action('do_feed_atom', 'fb_disable_feed', 1);

Archives