Welcome to dtsn.co.uk, if your new here please subscribe to my feed
24/08/2007

This article has been updated please see here.

I have only just started using Wordpress and first on my list of things to do was to create a custom theme for the blog. Theme creation is simple in Wordpress just copy and paste your HTML code into another theme. The one area I did have trouble in was creating a menu which used the sliding doors principle.

Typically root navigation in Wordpress is achieved through wp_list_pages which outputs a html list (<li>) of the pages complete with their links. However the sliding doors principle requires an extra tag in the list structure i.e. <span>.

Wordpress Outputs:

<li>
      <a href=<a href=”http://www.dtsn.co.uk”>www.dtsn.co.uk</a>
</li>

However we require:

<li>
     <a href=”http://www.dtsn.co.uk”><span>www.dtsn.co.uk</span></a>
</li>

To overcome this we use a little PHP and a SQL query to select all the pages from the database and display them in a custom list:

<?php
   $querystr = “SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status =
                    publish’ AND $wpdb->posts.post_type = ‘page’ ORDER BY $wpdb->posts.post_title ASC”

   $pageposts = $wpdb->get_results($querystr, OBJECT);
   if ($pageposts)
   {
      foreach ($pageposts as $post)
      {
         setup_postdata($post);
         $title = the_title(”,”,FALSE); //wp conditional statement
         ?>
           <li<?php
              if ( is_page($title) ) {
                ?> id=’selected’<?php
              } ?>>
              <a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”>
                 <span><?php the_title(); ?></span>
              </a>
           </li>
    <?php
    }
} ?>

The code above pulls out the database information regarding pages, we then call the corresponding wordpress functions (such as the_parmalink()) to insert the data in the correct place. The code above also applies a ’selected’ class to the menu item so that the user can determine which page they are on. Thus giving you the effect you see on this blog. This is done using one of Wordpress’s conditional statements (the_title()) which calls the title of the page you are currently on.

Promote
jameswillisisthebest

This is my first post
just saying HI

Susan

I’ve been looking for a way to use a sliding door navigation in Wordpress. Where would I place the code you posted?

Daniel

Hi Susan,

You would place the code into the HTML of your template file wherever you would like the menu to appear.

Let me know if this works, if not i could always send you an example.

Daniel

Susan

Thanks so much for your reply!

Just to be clear, this would work on these menus: http://exploding-boy.com/images/cssmenus/menus.html ?

But not these: http://www.cssplay.co.uk/menus/snazzymenu2.html ?

Daniel

They will actually work on both of them. Basically what you CSS wise is 2
elements. In my example I gave you 'a' and 'span', 'a' which you attach the left
image to, and span which you attach the right image to.

Since Stu Nicholls example doesn't use the sliding doors it method you have to
tweak the code a bit.

<code>
<li>
<a href=”<?php the_permalink() ? rel="nofollow">” rel=”bookmark”
title=”Permanent Link to <?php the_title(); ?>”>
<b class="snazzy">
<span class="boxcontent orange <?php if (is_page($title)) echo 'active'; ?><?php
the_title(); ?></span>
<b class="b4"></b>
<b class="b3"></b>
<b class="b2"></b>
<b class="b1"></b>
</a>
</li>
</code>
I think this will work, give it a try but i have just spewed it of the top of my
head.

Susan

Hi again,

I finally got around to trying this again. I’m having so much trouble. Here’s the error I get in Wordpress:

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' AND wp_posts.post_type = 'page' ORDER BY wp_posts.post_title ASC' at line 2]
SELECT wp_posts.* FROM wp_posts WHERE wp_posts.post_status = publish’ AND wp_posts.post_type = ‘page’ ORDER BY wp_posts.post_title ASC

Can you help?

Spencer

Hey! I’m so glad I found your site because I have literally been searching for a solution to this almost all day!

I was wondering if you could help me with this. I inserted your code into the header.php code where my navigation code is.

I put it in between the tags that I have for it, but it came back with an error.

Could you help me solve this problem? I would be so grateful for any help you could provide. Thanks!

Daniel

Could you please provide the error message?

Deacon

I’ve been wanting to do something like this with sliding doors and Wordpress for a while. — thanks for trying to figure it out. The code doesn’t work, however. When I try to use it, Wordpress returns the same error the others have experienced (unexpected variable). Any thoughts?

Daniel

@Deacon What is the unexpected variable?

Leland

Working on the navigation for the site I listed. Where do I put your above php code to make this work. Im dying. I want only the “current” button to have a background. I tried editing the classes.php file but that only partly fixed things.

Daniel

Hi Leland,

You stick it where you navigation goes, (so probably the header.php). This will create you the correct markup, then you apply the CSS. If you just want to make the item change css class look into that. It’s a lot simpler, and doesn’t require the hack i have above.

Daniel

dtsn : Wordpress Sliding Doors – take 2 [PHP]

[...] have had a lot of feedback over one of my first ever posts, Wordpress: Sliding Doors details how you can convert wp_list_categories into a subtle HTML markup so that we can use the [...]

Comment