<?php /** * The Header for our theme. * * Displays all of the <head> section and everything up till <div id=”main”> * * @package WordPress * @subpackage Twenty_Eleven * @since Twenty Eleven 1.0 */ global $is_iphone; if ( $is_iphone || $_GET[‘iam’]==’testing’ ) { include( STYLESHEETPATH . ‘/mobile/header.php’ ); } else { include( STYLESHEETPATH . ‘/inc_header.php’ ); } ?> Basically, the proper way to use $is_iphone. This example uses it to call two different header.php include files, but it could be used to call different css files or different template files, or whatever, basically. inc_header.php is the default header.php for …
Mullet
// /* This code goes in index.php */ <?php if ( have_posts() ) : ?> <?php /* Our Mullet Function begins here – Katie Suess 12/28/11 */ $classnum = count(get_body_class()); /* fetch body classes */ $classarr = get_body_class(); /* make it a testable variable */ for ($i = 0; $i <= $classnum; $i++) { $bodyclass = $classarr[$i]; if ($bodyclass == paged) { $excerpt_trigger = 1; /* No Mullet on Deeper Pages */ break 1; /* exit the while */ } else { $excerpt_trigger = 0; /* Set Count For Mullet */ } } ?> <?php /* Start the Loop */ …
Manual Canonical Redirects
If you need to make sure non-www links end up on www… #Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^yoursite.com [NC] RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301]
Show Featured Image
after setting featured image. parameter size accepts thumbnail, medium, large or custom eg. array(230,234) <?php $theattr = array( ‘src’ => $src, ‘class’ => “attachment-$size”, ‘alt’ => trim(strip_tags( $attachment->post_excerpt )), ‘title’ => trim(strip_tags( $attachment->post_title )), ); the_post_thumbnail( ‘large’, $theattr ); ?>
get a single row from a table in the db
versatile way to get single piece of info related to something you already have $user = $wpdb->get_row($wpdb->prepare(“SELECT * FROM $wpdb->users WHERE user_activation_key = %s AND user_login = %s”, $key, $login)); if ( !empty( $user ) ){ $username = $user->name; }
Truncate Text
put in functions to truncate a post excerpt or something <?php function ttruncat($text,$numb) { if (strlen($text) > $numb) { $text = substr($text, 0, $numb); $text = substr($text,0,strrpos($text,” “)); $etc = ” …”; $text = $text.$etc; } return $text; } ?>
WP Nav Menu
place a nav menu anywhere <?php $navargs = array( ‘theme_location’ => , ‘menu’ => , ‘container’ => ‘div’, ‘container_class’ => , ‘container_id’ => , ‘menu_class’ => ‘menu’, ‘menu_id’ => , ‘echo’ => true, ‘fallback_cb’ => ‘wp_page_menu’, ‘before’ => , ‘after’ => , ‘link_before’ => , ‘link_after’ => , ‘depth’ => 0, ‘walker’ => ); ?> <div class=”access”> <?php wp_nav_menu($navargs); ?> </div>
Include a file
<?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; } ?>