BeardedWineGuy BeardedWineGuy - 2 years ago 83
PHP Question

Want to insert a logo as the middle menu item in Wordpress theme

EDIT: I adjusted my parameters to insert the logo directly into the menu, instead of padding a specific menu item. The padding method can easily drive a centered menu off-center. This insertion method should resolve that issue.

I'm working on a theme, and want to create a menu split by a logo. I realize I could just create two menus, but I want this to be as streamlined for the user as possible. I've already been able to get the number of items and target the menu item I want, but I'm not sure how to use my functions.php file to add the class "pad-item" to the


Here is what I have to find and target the specified item. All it's returning, though, is an index number of top level items.

$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object($locations['primary']);
$items = wp_get_nav_menu_items($menu->term_id);
$top_level = 0;
foreach ($items as $val) {
if ($val->menu_item_parent === '0') {
$index = round($top_level / 2) - 1;
return $index;

Any help would be greatly appreciated. Thanks.

Answer Source

I was able to figure out the issue, and I wanted to post my solution in case someone else was looking for the same answer.

function main( $items, $args ) {

    // Checks to see if the menu passed in is the primary one, and creates the logo item for it
    if ( $args->theme_location == 'primary' ) {
        $logo_item = '<li class="menu-item">' . get_logo() . '</li>';

    //Gets the location of the menu element I want to insert the logo before
    $index = round( count_top_lvl_items() / 2 ) + 1;
    //Gets the menu item I want to insert the logo before
    $menu_item = get_menu_item( $index );
    $insert_before = '<li id="menu-item-' . $menu_item->ID;

    $menu_update = substr_replace( $items, $logo_item, strpos( $items, $insert_before ), 0 );

    return $new_menu;

//Counts the number of top level items in the menu
function count_top_lvl_items() {
    $items = get_menu_items();
    $counter = 0;
    foreach ( $items as $val ) {
        if ( $val->menu_item_parent === '0' ) {
    return $counter;

//Returns the menu item to insert the logo before
function get_menu_item( $index ) {
    $items = get_menu_items();
    $counter = 0;
    foreach ( $items as $val ) {
        if ( $val->menu_item_parent === '0' ) {
        if ( $counter == $index ) {
            return $val;

//Returns the logo menu item. I have it separated because my theme allows for varied logos
function get_logo() {
    $home = get_option( 'home' );
    $logo = get_option( 'logo' );
    $logo_item = <<<EOD

        <div id="logo">
            <a href="$home">
                <img src="$logo" id="logo-img" alt=""/>

    return $logo_item;

function get_menu_items() {
    $locations = get_nav_menu_locations();
    $menu = wp_get_nav_menu_object( $locations['primary'] );
    $items = wp_get_nav_menu_items( $menu );
    return $items;

Please feel free to tell me if I missed something, or if this could be done in a different way.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download