script to generate custom html UL LI for wordpress pages

Discussion in 'HTML & Website Design' started by nirmala.prc, Jul 18, 2009.

  1. #1
    Hope this code is useful to WP designers,

    <?php
     $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'page' AND $wpdb->posts.post_parent = 0 ORDER BY $wpdb->posts.post_title ASC";
    
        $pageposts = $wpdb->get_results($querystr, OBJECT);
    	$var1 = 1;
    	global $wp_query;
    	$page_id = $wp_query->post->ID;
       
    	$arraycount = count($pageposts)-1;
        foreach ($pageposts as $post):
    	if ($post->ID == $page_id)
    	{
    		$curitem="currentItem";
    	}
    	?>
    	<?php if ($var1==1) 
    		{ 
    		?>
    	<li class="menuitemfirst <?php echo $curitem; ?>" ><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    	<?php } else if ($var1==$arraycount) {  ?>
    
    	<li class="menuitemlast <?php echo $curitem; ?>" ><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    
    	<?php } else { ?>
    	<li class="menuitem <?php echo $curitem; ?>" ><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    	<?php } ?>
        
    	<?php 
    		$var1++; 
    		$curitem ="";
    		endforeach;
    	?>
    	<?php
    	function get_page_id($page_name){
    	global $wpdb;
    	$page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
    	return $page_name;
    }
    ?>
    PHP:
    The advantage of this script is, the first LI will be having classname "firstitem" and last menu will also have its own class name. Additionally it also has highlight option class.
     
    nirmala.prc, Jul 18, 2009 IP