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.
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

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
