Useful WordPress Code Snippets

| September 20, 2012 | 0 Comments

Today i am starting a new series of posts related to code snippets on my blog and i would like to share some interesting and very useful WordPress code snippets in today’s post. You will certainly find these code snippets useful during WordPress theme development.

 1.Getting Menu items

The following code snippet will help you to get Menu items one by one.It allows you  to perform filtering and styling on each item.

 

$menuname='Main Menu';

$menu = wp_get_nav_menu_items($menuname);
foreach($menu as $item)
{

echo ''.$item->title.''; 
}

 

2.Custom Paging Based on Taxonomy

You can use the following code to implement your own custom paging of posts based on taxonomy types.It will even help you to just implement your own custom paging for WordPress Custom post types.

 

$menuname='Main Menu';

query_vars['paged'])) ? 3 : $wp_query->query_vars['paged'];
$var=$_SERVER['REQUEST_URI']; $pieces = explode("/", $var); $paged=$pieces[count($pieces)-2]; 

query_posts(array(
'post_type' => 'blog', // can be custom post type
'paged' => $paged, // set the current page
'posts_per_page' => 5,
'taxonomy'=>'blog-categories',
'term'=>$_GET['cat']

));

function pagination($prev = '«', $next = '»') {
    global $wp_query, $wp_rewrite;
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    $pagination = array(
        'base' => @add_query_arg('paged','%#%'),
        'format' => '',
        'total' => $wp_query->max_num_pages,
        'current' => $current,
        'prev_text' => __($prev),
        'next_text' => __($next),
        'type' => 'plain'
);
    if( $wp_rewrite->using_permalinks() )
        $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );

    if( !empty($wp_query->query_vars['s']) )
        $pagination['add_args'] = array( 's' => get_query_var( 's' ) );

    echo paginate_links( $pagination );
};

?>

 

3.Display Latest Custom Post Only

The following code can be used to display the latest custom post type only. You can tweak this code to display one ,two or more than two custom post types.

 1, 'post_type'=> 'introductions', 'order' => 'DESC' ,'orderby'=> 'post_date' );
					$postslist = get_posts($args);
					foreach ($postslist as $post) :
						setup_postdata($post);
					?>
  

 

4.Change Login and Admin page Logo


You can copy and paste the following code in functions.php file inside your theme folder to change logo of Login and Admin page

add_action('admin_head', 'my_custom_logo');
function my_custom_logo() {
echo '';

}

function my_login_logo() {
 echo '';
}

add_action('login_head', 'my_login_logo');

 

5.Display Archives

You can display archive of your posts by just putting the following code in your desired page.

', '

‘); ?> /* you can change the parameter to get monthy,quarterly or yearly archives */

 

6.Display Facebook Like Box

You can go to the following page to generate code of your facebook page like box.
Source

 

7.Showing Popular Posts in your page

You can use the following code to Show popular posts with thumbnails in your page.


        have_posts()) : $my_q->the_post(); ?>

      • Posted on with

 

 


Source

 

9.Display Random Posts

You can use the following code to display random posts in your page.

 

$rand_posts = get_posts(‘numberposts=5&orderby=rand’);
foreach( $rand_posts as $post ) :
?>

9.Adding Option Panel for your WordPress Theme

You can easily add and use Option panel to your WordPress theme.OptionTree is a free plugin that allows you to add and use different option panels in your page. You can refer to my following post to configure option tree easily.
Source

 

10.Embed a page inside a page

You can use the following to code to embed a page inside a page.

have_posts()) : $post->the_post();?>

Tags: ,

Category: General Articles

Leave a Reply