Skip to content

Instantly share code, notes, and snippets.

@diggeddy
Last active February 13, 2023 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diggeddy/f38376cda0670fdbf8cb7de3ddc48f1d to your computer and use it in GitHub Desktop.
Save diggeddy/f38376cda0670fdbf8cb7de3ddc48f1d to your computer and use it in GitHub Desktop.
Menu image and subtitle
<?php
// simple function to insert post meta
// subtitle and menu_image
// in to sub menu items of primary navigation
function db_menu_insert_post_meta($sorted_menu_objects, $args) {
// select the menu
if ($args->theme_location === 'primary'){
// get the menu objects
foreach ($sorted_menu_objects as $menu_object) {
// get any posttype menu item that are not parents
if ( in_array($menu_object->object, array('post', 'page', 'any_post_type'))
&& $menu_object->menu_item_parent != 0
) {
// get the custom content
$subtitle = get_post_meta($menu_object->object_id, 'subtitle', true);
$menu_image = get_post_meta($menu_object->object_id, 'menu_image', true);
$thumbnail = wp_get_attachment_image($menu_image, 'thumbnail', false, array( 'class' => 'menu-thumbnail' ));
// build the custom classes and menu item
if ($subtitle || $thumbnail){
$menu_object->classes[] = 'custom-menu';
if ($subtitle){$menu_object->classes[] = 'has-subtitle';}
if ($thumbnail){$menu_object->classes[] = 'has-thumbnail';}
$menu_object->title = sprintf(
'%1$s %2$s %3$s',
$thumbnail,
'<span class="menu-item-label">'.$menu_object->title.'</span>',
$subtitle ? '<span class="menu-subtitle">' . $subtitle . '</span>' : ''
);
}
}
}
}
return $sorted_menu_objects;
}
add_filter('wp_nav_menu_objects', 'db_menu_insert_post_meta', 10, 2);
// basic CSS Styling
.main-nav ul li.custom-menu a {
display: flex;
flex-direction: column;
flex-wrap: wrap;
max-height: 60px;
align-content: flex-start;
justify-content: center;
}
.menu-thumbnail {
width: 36px;
border-radius: 100%;
margin-right: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment