Return to Snippet

Revision: 42137
at March 1, 2011 20:24 by daniel6442


Updated Code
function navigation() {
	global $wpdb, $post;
	
	$query = $wpdb->get_results("SELECT ID, post_title FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_type = 'page' AND post_parent = 0");
	
	echo '<div id="nav">';
	echo '<ul>';
	
	foreach ($query as $obj) {

		echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$obj->ID.'">'.$obj->post_title.'</a></li>';
		
		$sub_query = $wpdb->get_results("SELECT ID, post_title, post_parent FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_type = 'page' AND post_parent = {$obj->ID}");
		
		foreach($sub_query as $sub_obj) {
			if($post->ID == $sub_obj->post_parent)
  				echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$sub_obj->ID.'" class="subpage">'.$sub_obj->post_title.'</a></li>';
  				
  			if($post->post_parent == $sub_obj->post_parent)
  				echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$sub_obj->ID.'" class="subpage">'.$sub_obj->post_title.'</a></li>';
  		}	

  		
  	}
	
	echo '</ul>';
	echo '</div>';
}

Revision: 42136
at March 1, 2011 20:21 by daniel6442


Updated Code
function navigation() {
	global $wpdb, $wp_query, $post;
	
	$query = $wpdb->get_results("SELECT ID, post_title FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_type = 'page' AND post_parent = 0");
	
	echo '<div id="nav">';
	echo '<ul>';
	
	foreach ($query as $obj) {

		echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$obj->ID.'">'.$obj->post_title.'</a></li>';
		
		$sub_query = $wpdb->get_results("SELECT ID, post_title, post_parent FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_type = 'page' AND post_parent = {$obj->ID}");
		
		foreach($sub_query as $sub_obj) {
			if($post->ID == $sub_obj->post_parent)
  				echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$sub_obj->ID.'" class="subpage">'.$sub_obj->post_title.'</a></li>';
  				
  			if($post->post_parent == $sub_obj->post_parent)
  				echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$sub_obj->ID.'" class="subpage">'.$sub_obj->post_title.'</a></li>';
  		}	

  		
  	}
	
	echo '</ul>';
	echo '</div>';
}

Revision: 42135
at March 1, 2011 20:20 by daniel6442


Updated Code
function navigation() {
	global $wpdb, $wp_query, $post;
	
	$query = $wpdb->get_results("SELECT ID, post_title FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_type = 'page' AND post_parent = 0");
	
	echo '<div id="nav">';
	echo '<ul>';
	
	foreach ($query as $obj) {

		echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$obj->ID.'">'.$obj->post_title.'</a></li>';
		
		$sub_query = $wpdb->get_results("SELECT ID, post_title, post_parent FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_type = 'page' AND post_parent = {$obj->ID}");
		
		foreach($sub_query as $sub_obj) {
			if($post->ID == $sub_obj->post_parent)
  				echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$sub_obj->ID.'" class="subpage">'.$sub_obj->post_title.'</a></li>';
  				
  			if($post->post_parent == $sub_obj->post_parent)
  				echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$sub_obj->ID.'" class="subpage">'.$sub_obj->post_title.'</a></li>';
  		}	

  		
  	}
	
	echo '<li><a href="'.get_bloginfo('wpurl').'/medlem/login.php">Medlem</a></li>';
	
	echo '</ul>';
	echo '</div>';
}

Revision: 42134
at February 28, 2011 03:42 by daniel6442


Initial Code
function navigation() {
	global $wpdb, $wp_query, $post;
	
	$query = $wpdb->get_results("SELECT ID, post_title FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_type = 'page' AND post_parent = 0");
	
	echo '<div id="nav">';
	echo '<ul>';
	
	foreach ($query as $obj) {
  		echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$obj->ID.'">'.$obj->post_title.'</a></li>';
  		
  		if($wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}posts WHERE post_parent = ".$post->ID) > 0) {
  		
  			$subpages = $wpdb->get_results("SELECT ID, post_title FROM {$wpdb->prefix}posts WHERE post_type = 'page' AND post_status = 'publish' AND ".
  			 							   "post_type = 'page' AND post_parent = ".$obj->ID);
  		
  			foreach($subpages as $subpage) {
  			
  				if($post->post_parent == $wpdb->get_var($wpdb->prepare("SELECT post_parent FROM {$wpdb->prefix}posts ".
  																	   "WHERE post_type = 'page' AND ID = {$post->ID} AND post_parent = ".$obj->ID)) && is_subpage()) {
  						echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$subpage->ID.'" class="subpage">'.$subpage->post_title.'</a></li>';
  				}
  				
  				if($wpdb->get_var("SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'page' AND post_parent = ".$post->ID) == $subpage->ID) {
  					echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$subpage->ID.'" class="subpage">'.$subpage->post_title.'</a></li>';
  				}
  				
  			}
  			

  		}

  	}
	
	echo '<li><a href="'.get_bloginfo('wpurl').'/medlem/login.php">Medlem</a></li>';
	
	echo '</ul>';
	echo '</div>';
}

function is_subpage() {
	global $post, $wpdb;

	if ( is_page() AND isset( $post->post_parent ) != 0 )
	{
		$aParent = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d AND post_type = 'page' LIMIT 1", $post->post_parent ) );
		if ( $aParent->ID ) return true; else return false;
	}
	else
	{
		return false;
	}
}

Initial URL

                                

Initial Description

                                

Initial Title
Display subpages when parent-/subpage is opened in Wordpress

Initial Tags
php, wordpress, navigation

Initial Language
PHP