Posts

Add star ratings for products in woocommerce

add_action('woocommerce_after_shop_loop_item','get_star_rating');function get_star_rating(){global $woocommerce, $product; $average = $product->get_average_rating(); echo '<div class="star-rating"><span style="width:'.(( $average /5)*100).'%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__('out of 5','woocommerce').'</span></div>';} or
<divclass="product-rating"><?php

Add custom class in wordpress menu

First of all, you need to create a custom navigation menu fromAppearance -> Menus. Then, use thewp_nav_menuwith the following parameters: <?php $args = array('menu_class'=>'nav nav-tabs','menu'=>'(your_menu_id)'); wp_nav_menu( $args );?>or
wp_nav_menu( array('theme_location'=>'top-menu','container'=>'ul','menu_class'=>'add-your-class-here'));

To know more, visit: http://stackoverflow.com/questions/22742630/wp-nav-menu-add-class-on-ul/

Create Custom Post Types in WordPress

Add below code to your theme's functions.php


// Register Custom Post Type
function custom_post_type() {
    $labels = array(
        'name'                  => _x( 'Services', 'Post Type General Name', 'novamovers' ),
        'singular_name'         => _x( 'Service', 'Post Type Singular Name', 'novamovers' ),
        'menu_name'             => __( 'Services', 'novamovers' ),
        'name_admin_bar'        => __( 'Services', 'novamovers' ),
        'archives'              => __( 'Service Archives', 'novamovers' ),
        'attributes'            => __( 'Service Attributes', 'novamovers' ),
        'parent_item_colon'     => __( 'Parent Service:', 'novamovers' ),
        'all_items'             => __( 'All Service', 'novamovers' ),
        'add_new_item'   …

Redirect non-www to www in .htaccess

Change your configuration to this (add a slash): RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule (.*) http://www.example.com/$1 [R=301,L] Or the solution outlined below (proposed by@broken-mirror) will work for any domain: RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] If you need to support http and https and preserve the protocol choice try the following: RewriteRule ^login\$ https://www.%{HTTP_HOST}/login [R=301,L] To know more visit: http://stackoverflow.com/questions/12050590/redirect-non-www-to-www-in-htaccess

Remove slug from custom post type post URLs

Add given functions in your active theme's functions.php
/** * Remove the slug from published post permalinks. Only affect our custom post type, though. */ function gp_remove_cpt_slug( $post_link, $post, $leavename ) { if ( 'race' != $post->post_type || 'publish' != $post->post_status ) { return $post_link; } $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); return $post_link; } add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );/** * Have WordPress match postname to any of our public post types (post, page, race) * All of our public post types can have /post-name/ as the slug, so they better be unique across all posts * By default, core only accounts for posts and pages where the slug is /post-name/ */ function gp_parse_request_trick( $query ) { // Only noop the main query if ( ! $query->is_main_query() ) return; // Only …

Remove WordPress Redirecting to Nearest Matching URL

WordPress comes with a nifty feature which enables it to navigate to the closest matching URL if the one typed in doesn’t exist. This reduces the chance of a user getting presented with a 404 page if they’ve slightly mistyped a page’s URL.
Yes, the above is great, but what if we want to disable it? On a site recently I needed to do exactly this when I was using URL’s that didn’t relate to pages and, instead, jumped to different parts of a single page site.
The solution was to add the below code to my theme’sfunctions.phpfile:
remove_filter('template_redirect', 'redirect_canonical');