Posted By

scitrenbaumgmailcom on 07/12/10


Tagged


Versions (?)

Wordpress Adding Sub-Titles To Menu Links


 / Published in: PHP
 

  1. 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.
  2.  
  3. // Adds descriptive text to link titles
  4. // With help from http://blog.clearskys.net/2008/12/17/how-to-adding-menu-sub-titles-to-a-theme/
  5. function sub_page_list() {
  6. global $wpdb;
  7. $sql = "SELECT p.ID, p.post_title, p.guid, pm.meta_value FROM " . $wpdb->posts . " AS p LEFT JOIN ";
  8. $sql .= "(SELECT post_id, meta_value FROM " . $wpdb->postmeta . " AS ipm WHERE meta_key = 'subtitle') ";
  9. $sql .= "AS pm ON p.ID = pm.post_id ";
  10. $sql .= "WHERE p.post_type = 'page' AND p.post_parent = 0 AND p.post_status = 'publish' ";
  11. $sql .= "ORDER BY p.menu_order ASC ";
  12. $sql .= "LIMIT 0, 10";
  13. $rows = $wpdb->get_results($sql,OBJECT);
  14. if($rows) {
  15. foreach($rows as $row) {
  16. echo "<li>";
  17. $link_url = get_permalink($row->ID);
  18. echo "<a href=\"$link_url\"" . "\">$row->post_title</a>";
  19. echo "<span style=\"display:block;\">$row->meta_value</span>";
  20. echo "</li>";
  21. }
  22. }
  23. }
  24.  
  25. // Filter the menu to add the list
  26. function childtheme_page_menu() { ?>
  27. <div class="menu">
  28. <ul>
  29. <?php if (is_front_page()) { ?>
  30. <li><a href="<?php bloginfo('home') ?>/" title="<?php echo wp_specialchars( get_bloginfo('name'), 1 ) ?>" rel="home">
  31. Home <span style="display:block;">This is the home page</span>
  32. </a></li>
  33. <?php } else { ?>
  34. <li><a href="<?php bloginfo('home') ?>/" title="<?php echo wp_specialchars( get_bloginfo('name'), 1 ) ?>" rel="home">
  35. Home <span style="display:block;">Return to the home page</span>
  36. </a></li>
  37. <?php } ?>
  38.  
  39. <?php sub_page_list(); ?>
  40.  
  41. </ul>
  42. </div>
  43. <?php }
  44. add_filter('wp_page_menu','childtheme_page_menu');

Report this snippet  

You need to login to post a comment.