Yesterday, I covered five WordPress Code snippets to love and I hope you love these, today, just as much.
These aren’t the best 10 WordPress code snippets that I’ve gathered these past two days, but those that one should usually use on a fairly regular basis (aside from the more obvious ones?).
These lists weren’t certainly meant to be comprehensive or anything, so if you have any you would like to throw into the hat, be sure to leave them in the comments.
We would love to see them!
1. Custom Menu
Ever since WordPress 3.0, menus are much easier to add and edit by the user.
You’ll use it at least once, maybe more, if you have some extra places you drop-in extra menus.
Code
[cc lang=”php”] ‘Navigation’, ‘sort_column’ => ‘menu_order’ ) ); ?>[/cc]
If you are building a theme to offer as a download, then you will likely use the theme_location parameter so that users can specify their own menu/multiple menus.
Custom Menu’s. Love’em.
2. Dynamic Sidebar
This depends how fancy you’re building your theme, but if you’re going to add a sidebar, it might as well be dynamic!
Code
[cc lang=”php”][/cc]
Of course, don’t forget to drop this in your functions.php file.
[cc lang=”php”] ‘
‘,
‘before_title’ => ‘
‘,
‘after_title’ => ‘
‘,
))
;
?>[/cc]
And now you’re stylin’!
3. Get Header/Footer/Sidebar
Memorize these.
Code
[cc lang=”php”]
[/cc]
If you want your pages to display different sidebars, headers or footers than your blog posts or specific pages, this is what you’re going to use.
Example
[cc lang=”php”][/cc]
Of course, be sure you have your sidebar-extra.php setup, first.
4. Auto-Updating Date
Great theme development is all about the details. With this snippet, your copyright year will auto-update.
Code
[cc lang=”php”]© ORGANIZATIONNAME. All rights reserved.[/cc]
Done.
5. List Recent and Random Posts
This is great for front pages, while the random posts can be good to list at the end of your posts, too.
It’s all up to your imagination!
List Recent Code
[cc lang=”php”][/cc]
Take a look at the various parameters on the WordPress Codex.
List Random Code
[cc lang=”php”]
- 5, ‘orderby’ => ‘rand’ );
- WordPress Codex.
Drop any of your favorite WordPress snippets off in the comments!
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
James Cooper says
I love the new custom menus as well! If you want more than one you need to register them in functions.php like:
add_action( ‘init’, ‘register_my_menu’ );
function register_my_menu() {
register_nav_menu( ‘primary-menu’, __( ‘Primary Menu’ ) );
register_nav_menu( ‘secondary-menu’, __( ‘Secondary Menu’ ) );
}
Then you can call them in the theme like:
‘Navigation’, ‘sort_column’ => ‘menu_order’, ‘theme_location’ => ‘primary-menu’ ) ); ?>
Eric Dye says
Sweet! Thanks for adding some more chops, James!
Eric J says
Is there a typo in #4? wouldn’t it say copywrite (c) 2011-2011? Also i was told that the proper way to cite copywrite is:
© ORGANIZATIONNAME. All rights reserved.
Eric Dye says
You are right. Updated!