Get first image attached to a post

Useful:

function get_post_image($size = 'thumbnail') {
	global $post;
	$photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
	if ($photos) {
		$photo = array_shift($photos);
		return wp_get_attachment_image($photo->ID, $size);
	}
	return false;
}

Continue Reading →

WordPress: get page link by ID

This function is used to get the_permalink() of a certain page using it’s ID (outside the loop). To use it within a theme, just:

<?php getPageLink(ID); ?>
function getPageLink($pageId){
   if(!is_numeric($pageId)) {
      return;
   }
   global $wpdb;
   $sql_query = 'SELECT DISTINCT * FROM ' . $wpdb->posts . ' WHERE ' . $wpdb->posts . '.ID=' . $pageId;
   $posts = $wpdb->get_results($sql_query);
   if(!empty($posts)) {
      foreach($posts as $post) {
         return nl2br($post->guid);
      }
   }
}

WordPress: Get Page Title by ID

This function is used to get the title(); of a certain page using it’s ID (outside the loop). To use it within a theme, just:

<?php getPageTitle(ID); ?>
function getPageTitle($pageId){
	if(!is_numeric($pageId)) {
		return;
	}
	global $wpdb;
	$sql_query = 'SELECT DISTINCT * FROM ' . $wpdb->posts . ' WHERE ' . $wpdb->posts . '.ID=' . $pageId;
	$posts = $wpdb->get_results($sql_query);
	if(!empty($posts)) {
		foreach($posts as $post) {
			return nl2br($post->post_title);
		}
	}
}