search ]

Archives: Snippets | Page 12

Display Tag Cloud in WordPress Templates

Although we see less and less use of tag clouds on WordPress sites, here is how to add one using code in WordPress templates. This is the same tag cloud that appears in the widgets area of the WordPress admin.

Add the following code where you want in your template and change the parameters as needed:

<?php wp_tag_cloud(array(
     'smallest' => 10, // size of least used tag
     'largest' => 18, // size of most used tag
     'unit' => 'px', // unit for sizing
     'orderby' => 'name', // alphabetical
     'order' => 'ASC', // starting at A
     'exclude' => 6 // ID of tag to exclude from list
     ));
?>

For more on tags and taxonomies, see Taxonomies in WordPress.

Show Post Thumbnails in RSS Feed

If you want to display the post featured image in the RSS feed (essential if you use RSS for Mailchimp for example), add the following code to your functions.php file:

// Put post thumbnails into rss feed
function savvy_feed_post_thumbnail($content) {
    global $post;
    if(has_post_thumbnail($post->ID)) {
        $content = '' . $content;
    }
    return $content;
}
add_filter('the_excerpt_rss', 'savvy_feed_post_thumbnail');
add_filter('the_content_feed', 'savvy_feed_post_thumbnail');

Change Payment Icons on WooCommerce Checkout

Here is an example of how to change the default PayPal icon on the WooCommerce checkout page. Live example:

<?php
/**
 * Add custom gateway icons
 * 
 * @param  string $icon       Image HTML.
 * @param  string $gateway_id Gateway ID.
 * 
 * @return string
 */
function custom_wc_gateway_icons( $icon, $gateway_id ) {
	// Example for PayPal:
	if ( 'paypal' == $gateway_id ) {
		$icon = '<img src="' . WC_HTTPS::force_https_url( 'http://your-site.com/image.png' ) . '" alt="' . __( 'PayPal' ) . '" />'
	}
	return $icon;
}
add_filter( 'woocommerce_gateway_icon', 'custom_wc_gateway_icons', 10, 2 );

For a complete WooCommerce setup guide, see Create a Virtual Store with WooCommerce.

Change Number of Items on WooCommerce Orders Page

To change the number of items on the WooCommerce orders page (on the front end), use the following code where X is the maximum number of items to display (functions.php):

function custom_my_account_orders( $args ) {
    $args['posts_per_page'] = X;
    return $args;
}
add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_account_orders', 10, 1 );

More on managing and handling orders in WooCommerce sites in the post Managing orders in WooCommerce.

Add a Link to Yoast SEO Breadcrumbs

Say you want to add a link to the blog in the Yoast breadcrumbs after the initial link to the home page. You can do this by adding the following code to your functions.php file:

function yoast_seo_breadcrumb_append_link( $links ) {
    global $post;
        $breadcrumb[] = array(
            'url' => site_url( '/blog/' ),
            'text' => 'Blog',
        );
        array_splice( $links, 1, -2, $breadcrumb );
    return $links;
}
add_filter( 'wpseo_breadcrumb_links', 'yoast_seo_breadcrumb_append_link' );

Here is a broader article on how to add breadcrumbs to WordPress sites

Increase Maximum Execution Time in WordPress

You may have encountered the message “Maximum execution time of 30 seconds exceeded“. This message means the operation you are performing takes longer to complete than allowed. There are several ways to handle this:

1. Edit wp-config.php

Add the following code to wp-config.php:

set_time_limit(200);

2. Edit the .htaccess file

Make sure you back up this file first and add the following code:

php_value max_execution_time 200

3. Edit the php.ini file

Add the following line to your php.ini file:

max_execution_time = 200

If this does not work, try consulting with your hosting provider…

For more tips and options you can perform via the wp-config.php file, take a look at the post Optimizing WordPress configuration with wp-config.php.

Display Relative Dates in WordPress

Instead of displaying the exact date your post was published, you can display it relatively – “6 months ago” or “3 weeks ago”. Use the following code to do this:

# For posts & pages #
<?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?>

# For comments #
<?php echo human_time_diff(get_comment_time('U'), current_time('timestamp')) . ' ago'; ?>

For another useful date/time display, see Display Estimated Post Reading Time.

Display Total Post Count for a Custom Post Type

This short snippet lets you display the total number of posts for a specific Custom Post Type. Add the code where you want to display the post count and change POST-TYPE-NAME to the name of the post type whose count you want to show.

<?php
     // Get total number of posts in POST-TYPE-NAME
     $count_posts = wp_count_posts('POST-TYPE-NAME');
     $total_posts = $count_posts->publish;
     echo $total_posts;
?>
Savvy WordPress Development official logo