<?php include(TEMPLATEPATH . ‘/includes/featured.php’); ?> If using a child theme use STYLESHEETPATH instead <?php include(STYLESHEETPATH . ‘/includes/featured.php’); ?>
Show all errors
if you get the white screen in WordPress, try this in wp-config.php but keep it commented out most of the time to hide misc. errors from normal viewers define(‘WP_DEBUG’, true);
MENU – show class in wp_list_pages for links that have kids
This comes in handy if you are using a list_page menu and need to change the style of menu elements that have children, liks add an arrow. This needs to be in the functions file. add_filter(‘page_css_class’, ‘nice_navigation_page_css_class’, 10, 2); /** * adds class “page-has-children” to all pages that have children * @param array $class. The page css class being modified, passed as an array from Walker_Page * @param object $page. The page object passed from Walker_Page * @return array Returns the new page css class. */ function nice_navigation_page_css_class($class, $page) { // check if current page has children $children = get_pages(‘child_of=’.$page->ID); …
MENU – show class in wp_list_pages for links that have kidsRead More
Show Logged in User Info
<?php global $current_user; get_currentuserinfo(); echo ‘Username: ‘ . $current_user->user_login . “n”; echo ‘User email: ‘ . $current_user->user_email . “n”; echo ‘User first name: ‘ . $current_user->user_firstname . “n”; echo ‘User last name: ‘ . $current_user->user_lastname . “n”; echo ‘User display name: ‘ . $current_user->display_name . “n”; echo ‘User ID: ‘ . $current_user->ID . “n”; ?>
Show all sql errors
add this right after the sql statement line $wpdb->show_errors();
Use a shortcode in a template
<?php do_shortcode(‘[shortcode]’); ?>
Update a single row in the db.
<?php $wpdb->query( ” UPDATE $wpdb->posts SET post_parent = 7 WHERE ID = 15 AND post_status = ‘static’ ” ); ?>
Blog Info Tags
Usually needed for displaying the stylesheet directory for use in file paths, put lots of other good uses as well Format <?php bloginfo(‘stylesheet_directory’); ?> Complete List – follow the same format as above name = Testpilot description = Just another WordPress blog admin_email = admin@example url = https://example/home wpurl = https://example/home/wp stylesheet_directory = https://example/home/wp/wp-content/themes/child-theme stylesheet_url = https://example/home/wp/wp-content/themes/child-theme/style.css template_directory = https://example/home/wp/wp-content/themes/parent-theme template_url = https://example/home/wp/wp-content/themes/parent-theme atom_url = https://example/home/feed/atom rss2_url = https://example/home/feed rss_url = https://example/home/feed/rss pingback_url = https://example/home/wp/xmlrpc.php rdf_url = https://example/home/feed/rdf comments_atom_url = https://example/home/comments/feed/atom comments_rss2_url = https://example/home/comments/feed charset = UTF-8 html_type = text/html language = en-US text_direction = ltr version = 3.1
Show All Query Vars
This is great for troubleshooting. If you need to see the main variables WP is loading, try sticking this in your template and refreshing. For more info, try this without the ->query_vars at the end. <pre><?php print_r($wp_query->query_vars); ?></pre>
List Subpages
This is great for custom sub nav of pages <?php // list the About subpage menu $args=array( ‘showposts’=>5, // how many to show ‘sort_column’=>’menu_order’, ‘post_type’=>’page’, // page or post ‘child_of’ => $post->ID, ); $myPosts = get_pages($args); if ($myPosts) { foreach ($myPosts as $myPage) { $permalink = get_permalink($myPage->ID); } } ?>
Display Content of a post or page anywhere
This is useful if you have a place on the sidebar or a box on the homepage where you want to have some changeable content. Add the content to a post or page and then output it wherever you want it using this. <?php // know the id of the page you want to display $theId = 668; // get the post $feature = get_post($theId); // get the content for the post. You can also get anything else for the post, post_title, post_excerpt… $content = $feature->post_content; echo $content; ?>
Outputting array elements from query_vars
If you use <pre><?php print_r($wp_query->query_vars); ?></pre> to display the query var array and decide to use one of the elements, here are some simple examples the get_query_var() function that outputs them. <?php $curcat = get_query_var(‘category_name’); $curtax = get_query_var(‘taxonomy’); $curterm = get_query_var(‘term’); ?>
Custom Database Query – WP Style
Get just about anything and display it. <?php $sql = “SELECT event_begin, event_end, event_title, event_short FROM wp_my_calendar ORDER BY event_begin LIMIT 3”; $holidays = $wpdb->get_results($sql); if ($holidays) { foreach ($holidays as $holiday) { $eventstart = $holiday->event_start; } ?>
Simple Mail Function
<?php wp_mail(‘jcamomile@pobox.com’, ‘test’, ‘The message’); ?>
Remove a filter
If you want to prevent a plugin or function from working on this template only, you can use this to remove the filter. Of course you need to find out the name of the filter first. This can be done by looking for the the add_filter functions in plugin. The example below removes the st_add_widget (share this) from the content <?php remove_filter(‘the_content’, ‘st_add_widget’); ?>
Echo Custom Field (Post Meta)
<?php echo get_post_meta($post->ID, ‘customfield’, true); ?>
What Template am I in?
Use in functions php. It outputs the name of the template file being used to display the current page. add_action(‘wp_head’, ‘show_template’); function show_template() { global $template; print_r($template); }
Check if user is Admin
Make something visible or invisible depending on whether logged in user is admin: <?php if ( is_user_logged_in() && current_user_can(‘manage_options’) ) { do_something(); } ?>
Links List (bookmarks) Advanced
Need to List a bunch of links, aka bookmarks, aka blogroll … This code comes in handy <?php $args = array( ‘orderby’ => ‘name’, ‘order’ => ‘ASC’, ‘limit’ => -1, ‘category’ => , ‘category_name’ => , ‘hide_invisible’ => 1, ‘show_updated’ => 0, ‘include’ => , ‘exclude’ => , ‘search’ => ); ?> <?php $bookmarks = get_bookmarks( $args ); // Loop through each bookmark and print formatted output foreach ( $bookmarks as $bm ) { printf( ‘<a class=”relatedlink” href=”%s”>%s</a><br />’, $bm->link_url, __($bm->link_name) ); } ?>
Hide Development Code before publishing
Cute way to partition off code that you are working on so that normal users don’t see it but you do. Simply add this conditional around your test code. Then on the test page, add ?iam=testing to the end of the url. <?php if ($_GET[‘iam’]==’testing’){ // code you are testing } ?>