• home
  • forum
  • my
  • kt
  • download
  • Complex styled menu through custom fields

    Author: 2008-08-16 09:36:36 From:

    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:

    <?php white_page_menu() ?>
    

    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!

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (14)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot