16 Apr 2010

Style wp_list_pages

, ,

wp_list_pages() generates an HTML list of all the pages on WordPress. This tutorial will explain how to style that list to have a nice image menu rather than plain text. This way, you won't have to manually code every link. First of all, you will need to insert that code wherever you want your menu to appear:

<ul id="menu">
  <?php wp_list_pages('title_li=&depth=1&link_before=<span>&link_after=</span>'); ?>
<ul>

The generated HTML code should look like this:

<ul id="menu">
  <li class="page_item page-item-1"><a href="lorem.html" title="Lorem"><span>Lorem</span></a></li>
  <li class="page_item page-item-2"><a href="ipsum.html" title="Ipsum"><span>Ipsum</span></a></li>
  <li class="page_item page-item-3"><a href="dolor.html" title="Dolor"><span>Dolor</span></a></li>
  <li class="page_item page-item-4"><a href="amet.html" title="Amet"><span>Amet</span></a></li>
</ul>

We will have to style all that in our stylesheet. Let's start by removing some padding and margin:

ul#menu {
  margin: 0px;
  padding: 0px;
  list-style: none;
  width: 200px; /* menu width */
}

<span> and </span> that we had inserted by wp_list_pages() will hide the link text so that we can use images instead:

ul#menu li.page_item a span {
  visibility: hidden;
}

Now we will write all the styles shared by all links:

ul#menu li.page_item a {
  display: block; /* so that the whole image is clickable */
  text-decoration: none; /* remove link underline */
  width: 200px; /* if your images have the same width and height, define them here */
  height: 40px; /* if not, individually define them in each link class */
  margin-bottom: 10px; /* add a 10px margin between the links */
}

We are now going to define images for each link. As you can see, WordPress adds an unique CSS class for every link: page-item-1, page-item-2, etc. The number is the ID of the page so you will have to know your pages IDs but this is easy to get. There are two ways to style the links: one using IDs, one using pages titles. I'll start with the ID method.

Remember, the HTML code generated by wp_list_pages() is like this for every link:

<li class="page_item page-item-1"><a href="lorem.html"  title="Lorem"><span>Lorem</span></a></li>

page-item-1 is variable and different for each link, this allows us to style each link differently:

ul#menu li.page-item-1 a {
  background: url(images/lorem.jpg) no-repeat;
}
ul#menu li.page-item-2 a {
  background: url(images/ipsum.jpg) no-repeat;
}
ul#menu li.page-item-3 a {
  background: url(images/dolor.jpg) no-repeat;
}
ul#menu li.page-item-4 a {
  background: url(images/amet.jpg) no-repeat;
}

The other method; we also see that the HTML generated uses the title attribute for each link:

<li class="page_item page-item-1"><a href="lorem.html"  title="Lorem"><span>Lorem</span></a></li>

Titles are different for every pages, so we can use this to style all the links differently:

ul#menu li a[title=Lorem] { /* Get all links with title "Lorem" */
  background: url(images/lorem.jpg) no-repeat;
}
ul#menu li a[title=Ipsum] {
  background: url(images/ipsum.jpg) no-repeat;
}
ul#menu li a[title=Dolor] {
  background: url(images/dolor.jpg) no-repeat;
}
ul#menu li a[title=Amet] {
  background: url(images/amet.jpg) no-repeat;
}

That's it, quite easy no?

14 thoughts on “Style wp_list_pages

  • Everything works except this part:

    ul#menu li.page_item a span {
    visibility: hidden;
    }

    Text links are showed!?
    Dejan September 13, 2010 16:03

    Reply
    • Yes, sorry, the wp_list_pages code was wrong, use this instead :

      <?php wp_list_pages('title_li=&depth=1&link_before=<span>&link_after=</span>'); ?>
      Karine September 13, 2010 16:11

  • Mortel !
    ça marche impécablement bien...
    si ce n'est que l'onglet "Accueil" n'apparait plus dans le menu
    et ça c'est vraiment regretable.
    :/
    franck September 15, 2010 10:03

    Reply
    • hm quel onglet accueil ? il faut le styler lui aussi
      Karine September 15, 2010 16:26

  • Thank you so much! Merci mille-fois! It's exactly what I was looking for.

    The funny thing is I might not need it anymore since I've followed your recommendation and took a closer look at CushyCMS... :-)

    Kind Regards from Hamburg,

    Georg
    Georg September 19, 2010 1:33

    Reply
  • Merci beaucoup, j'en ai eu besoin aussi !


    ben October 14, 2010 23:50

    Reply
  • OMG ! Je t'aime.
    KRL November 16, 2010 18:17

    Reply
    • Contente d'avoir été utile ! :D
      Karine November 25, 2010 7:51

  • thats very nice.. im glad i found your tutorial. thanks keep it coming. Mabuhay ka!
    gerald July 21, 2011 16:38

    Reply
  • Super, ça m'aide beaucoup.
    Comment faire la même chose en horizontal.
    J'ai essayé :

    #departements ul {
    margin: 0px;
    padding: 0px;
    list-style: none;
    width: 384px;
    }

    #departements li.page_item a span {
    visibility: hidden;
    }

    #departements .page_item {
    display:inline;
    }

    #departements li.page_item a{
    display: inline-block;
    text-decoration: none;
    width: 128px;
    height: 128px;
    margin-left: 10px;
    }
    mais j'ai une des images qui s'affiche décalée à cause je pense d'un titre de page sur deux lignes.
    Comment faire ?
    Jenny November 10, 2011 1:46

    Reply
    • Salut Jenny, ce serait mieux que je puisse voir le bug en question pour t'aider... ^^
      Karine November 12, 2011 19:59

  • Bonjour tout ton tuto a bien marché et donc j'aimerais savoir s'il est possible de voir apparaitre le titre de la page grâce à un survol de l'image, car j'ai essayé plusieurs méthodes et pour l'instant pas une seule n'a marché.

    Merci d'avance pour vos réponses ^^
    Ken_la July 31, 2012 19:09

    Reply
    • Bonjour, le titre de la page est entre < span > et , on le cache avec le CSS. Tu peux le faire réapparaître au :hover par le css aussi.

      ul#menu li.page_item a:hover span {
      visibility: visible;
      }

      Avec du style supplémentaire sur le span, tu peux avoir un résultat bien. Sinon, tu peux aussi utiliser du javascript.
      Karine August 1, 2012 16:29

  • Je cherchais à utiliser la fonction "title" pour les liens et donc retoucher le code php mais cette technique me convient également donc merci beaucoup ^^
    Ken_la August 1, 2012 18:59

    Reply

Leave a Reply

get in touch

Always happy to receive an email!