Return to Snippet

Revision: 28674
at July 12, 2010 14:19 by scitrenbaumgmailcom


Initial Code
Usually this is done by hand editing the menu but it can be accomplished using WordPress custom fields. Simply add a custom field to the pages in question named “subtitle” with a value of “whatever you want your descriptive subtitle text to be”. Then make sure you have the following code snippet (adapted from the clearskys.net blog ) in the functions file of your WordPress Child Theme—as long as the parent theme is using wp_page_menu. If you’re simply editing a WordPress theme directly, cut the code from the childtheme_page_menu function and use it to replace your existing menu in header.php.

// Adds descriptive text to link titles
// With help from http://blog.clearskys.net/2008/12/17/how-to-adding-menu-sub-titles-to-a-theme/
function sub_page_list() {
	global $wpdb;
	$sql = "SELECT p.ID, p.post_title, p.guid, pm.meta_value FROM " . $wpdb->posts . " AS p LEFT JOIN ";
	$sql .= "(SELECT post_id, meta_value FROM " . $wpdb->postmeta . " AS ipm WHERE meta_key = 'subtitle') ";
	$sql .= "AS pm ON p.ID = pm.post_id ";
	$sql .= "WHERE p.post_type = 'page' AND p.post_parent = 0 AND p.post_status = 'publish' ";
	$sql .= "ORDER BY p.menu_order ASC ";
	$sql .= "LIMIT 0, 10";
	$rows = $wpdb->get_results($sql,OBJECT);
	if($rows) {
		foreach($rows as $row) {
			echo "<li>";
			$link_url = get_permalink($row->ID);
			echo "<a href=\"$link_url\"" . "\">$row->post_title</a>";
			echo "<span style=\"display:block;\">$row->meta_value</span>";
			echo "</li>";
		}
	}
}

// Filter the menu to add the list
function childtheme_page_menu() { ?>
    <div class="menu">
        <ul>
            <?php if (is_front_page()) { ?>
            <li><a href="<?php bloginfo('home') ?>/" title="<?php echo wp_specialchars( get_bloginfo('name'), 1 ) ?>" rel="home">
                Home <span style="display:block;">This is the home page</span>
            </a></li>
            <?php } else { ?>
            <li><a href="<?php bloginfo('home') ?>/" title="<?php echo wp_specialchars( get_bloginfo('name'), 1 ) ?>" rel="home">
                Home <span style="display:block;">Return to the home page</span>
            </a></li>
            <?php } ?>

            <?php sub_page_list(); ?>

        </ul>
    </div>
<?php }
add_filter('wp_page_menu','childtheme_page_menu');

Initial URL


Initial Description


Initial Title
Wordpress Adding Sub-Titles To Menu Links

Initial Tags


Initial Language
PHP