WordPress.org

Ready to get started?Download WordPress

Forums

Nav Menu Images
Adding description after title (2 posts)

  1. webrightnow
    Member
    Posted 3 months ago #

    I have been trying to modify the plugin code to add support for menu item descriptions below the titles.

    I borrowed part fo the code from this tutorial and replaced this bit of code in menu-image.php:

    $item_output = $args->before;
    		$attributes .= " class='{$attributes_classes}'";
    		$item_output .= '<a' . $attributes . '>';
    		$link = $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;

    with:

    $prepend = '<strong>';
            $append = '</strong>';
            $description  = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';
    
            if($depth != 0)
            {
    			$description = $append = $prepend = "";
            }
    		$item_output = $args->before;
    		$attributes .= " class='{$attributes_classes}'";
    		$item_output .= '<a' . $attributes . '>';
    		$item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
            $item_output .= $description.$args->link_after;

    This works in a way, because the description is now being outputted after the title as expected. The only problem is that this seems to break the image positioning function that follows it. Even if I choose to display the title after the image, the output always shows it before the image.
    How do I get the title positioning to work properly and still keep the description output?
    BTW the plugin still works flawlessly in WP 3.9, it would be good to add menu item description support in the next release.
    Thanks

    https://wordpress.org/plugins/nav-menu-images/

  2. webrightnow
    Member
    Posted 3 months ago #

    Never mind, I figured it out. I realized I had accidentally deleted the $link function that was being called by the title position switch, so I replaced it.
    I also got rid of the $prepend and $append functions which I don't really need as I don't want those titles to be bold. I ended up with this code in place of the one from my OP:

    $description  = ! empty( $item->description ) ? '<br/><span>'.esc_attr( $item->description ).'</span>' : '';
    $item_output = $args->before;
    $attributes .= " class='{$attributes_classes}'";
    $item_output .= '<a' . $attributes . '>';
    $link = $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $description . $args->link_after;

    This works perfectly as far as I can tell.

    Hope this helps anyone who might be interested in adding descriptions.

    The only problem is that now I won't be able to update the plugin, so I really hope some version of this code is added to the next release.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.