RSS feed

Nutsmuggling

Wordpress: Complex styled menu through custom fields

Custom fields are an amazing and often overlooked feature of Wordpress. The Wordpress codex example of the use of custom fields is’t very inspiring.

Currently Reading: Calvin and Hobbes
Today's Mood: Jolly and Happy

Knowing what someone was reading while writing a post might be interesting, and of course a knowledge of his mood might help to make sense of his rants but hey, is that all? After reading the Codex I archived custom fields in the “Might mean something but presently seems utterly pointless to me” section of my brain.

I had to realise the usefulness of custom fields later.
davidebenini.it, the professional section of this website, features a styled tab menu.

Personal details | Nutsmuggling.png

These tabs link to static pages; more correctly to all the children pages of “Home Page”. These pages’ title are often different from the tab’s label. Thus, “Web Design + Development” links to a page titled “Web Design and Development”. Also, note the different styles of the first and second line of the tab. How to get these tabs from a page dynamically? The answer is, of course, through custom fields.

My main pages feature a couple of custom fields:1

Nutsmuggling › Edit — WordPress-2.png

As you see, they correspond to the first and second line of the title. Setting these fields inside each main page, I can choose a tab-specific title, segmented in two lines.

And now to the code.
For long chunks of code I tend to use the functions.php file in the template folder. This file serves to store functions; you can use it to keep your template files clean.

function white_page_menu() {
    $menu = "<ul id='nav'>";
    $args = array(
    'post_type' => 'page',
    'numberposts' => 5,
    'post_parent' => 10,
    'order' => 'ASC',
    'orderby' => 'menu_order');
    $pageslist = get_posts($args);
    foreach ($pageslist as $post) {
        if (is_page($post->ID)) {
            $menu .= "<li style='display: inline' id='selected'>";
        } else {
            $menu .= "<li>";
        }
        $menu .= "<a href='?page_id=$post->ID'>";
        $custom_fields = get_post_custom($post->ID);
        $first_line_field = $custom_fields['title-first-line'];
        foreach ( $first_line_field as $key => $value )
        $translated_value= p__($value);
        $menu .= "<strong>$translated_value</strong>";
        $second_line_field = $custom_fields['title-second-line'];
        foreach ( $second_line_field as $key => $value )
            $menu .=  p__($value);
        $menu .="</a></li>" /* Fine menu-tab */;
    }
    $menu .="</ul>";
    echo $menu;
}

Then in my template file I just added:

<ul id='nav'><li><a href='?page_id=11'><strong>Personal</strong>details</a></li><li><a href='?page_id=8'><strong>Web Design</strong>+ development</a></li><li><a href='?page_id=9'><strong>Translation</strong>+ localisation</a></li><li><a href='?page_id=22'><strong>Academic</strong>career</a></li><li><a href='?page_id=23'><strong>Traditional</strong>music</a></li></ul>

As you see, my functions calls both title-first-line and title-second-line, and wraps their values into different style tags. This way, if I choose to add another main page to my website, I just have to make it a child of “home-page” and assign it some value to the title-first-line and title-second-line custom fields.

Of course this is just an example of the use of custom fields. You could use them to add a subtitle to you post, or define other specific value that you will call in your template.

Enjoy!

Notes

  1. I have deleted the extra fields for clarity’s sake. []
Share and Enjoy:
  • Digg
  • del.icio.us
  • Google
  • Technorati
  • StumbleUpon
  • Furl
  • Reddit

11 responses to “Wordpress: Complex styled menu through custom fields”

  1. Brian Richards says:

    You just saved my life. You have no idea how badly I was looking for exactly this tutorial!

    Be blessed! -Brian

  2. Brian Richards says:

    Hmm.. I jumped the gun a bit. There are a couple of tweaks I’d like to make to this, but am at a loss. Would you be willing to answer a quole of quick questions? If so, just shoot me an e-mail!

    Thanks again!

  3. Brian Richards says:

    And by “quole” I of course mean it’s non-typo equivalent “couple”

  4. Davide says:

    Thanks, it’s nice to get some feedback. Anyway, I’ve just sent you an e-mail, for any further question. Cheers, Davide

  5. Waseem says:

    Do you know what the selectors are for this menu to be styled in css. Could you give me an example maybe. thanks for the tutorial I was searching for this effet everywhere. Never really knew the power of custom fields lol

    thanks waseem

  6. Waseem says:

    also where exactly do you place

    the header.php file?

  7. Davide says:

    @ Waseem The header.php files goes inside your theme folder, and is invoked by the get_header(); function. As far as styling is concerned, it’s a bit complicated. That is what I’ll do: I will write a second part of this tutorial about css styling of the menu, to get horizontal tabs; I’ll take a few days, but I’ll hope it’ll be useful.

  8. antiquarian books says:

    Thank you for this information. :)

  9. Vince says:

    Cool stuff, I was just doing something similar, but your way is so much easier. If you don’t mind, I’d like to ask and see if you can help me out with this scenerio.

    So in my post, I’m using custom fields to add a specific type of content (sometimes image or video). To that, I also use the ‘nextpage’ feature in wordpress to break my post into multiple pages. My question, how do I show a different image or video through custom fields if the user visit the 2nd or 3rd page of the same post.

    Thanks!!

  10. flashrash says:

    Thanks! I will recommend this to all my friends.

  11. GrartPhampfaf says:

    http://edhardyshop.com/resources/edhardyshop.com/images/logo.gif

    ed hardy uhren ist Er ist aber auch der Mann, der lässt Truckercaps als Markenzeichen. ist, der Michael, P-Diddy und Geburtstag. Designs sind Rock?n brands4friends in Ed Hardy-Shoppingaktion ab 29. Mai kaufen können.

    So kürzlich Britneys Spears eine den ed hardy günstiger und erleichterte Audigier. Selbst das deutsche Top-Model Klum von dem ed hardy de Style und deshalb eine Staffel von Germany’s Next Topmodel, in der die nicht von Christian Audigier für gecastet werden. In der aktuellen 3. Staffel bekam 4 Kandidatinnen einen der Brand und Bill ed hardy parfum in zu sein.Mit bodenständigem und krachendem Rock’n ed hardy parfum Roll in Venen Label und schickt nun ed hardy de in den brands4freinds Onlien Shop. Vom 29. bis 31. Mai. können den Roll-Style von Ed Hardy bei. Reduzierte Preise sind. Wer jetzt Mitglied ist, brands4friends und mit ein bisschen man sich im Shopping-Club von brands4friends und ed hardy schuhe bei der Hardy-Shoes-Aktion mit dabei.

Leave a Reply