通过 functions.php 在菜单中使用自定义字段默认图像

Using a custom field default image in menu via functions.php

我在菜单的自定义字段中遇到问题,如果未选择图像,我想显示默认图像

注意:我想知道 funtions.php 方法不在 php 页面文件中。

这是我的代码。

            if ($image) {
                $item->title .= '<div class="thumb wrapme "><img src="' . $image['url'] . '" alt="' . $image['alt'] . '" /></div>';
            }else{
                $item->title .= 'This Should show up in relevent menu not in all';
            }

否则无法正常工作

它向所有菜单项显示错误。

它可以与 isset() 一起使用吗?

或者您是否需要在 function.php 覆盖中使用它?

if (isset($image)) {
    $item->title .= '<div class="thumb wrapme "><img src="' . $image['url'] . '" alt="' . $image['alt'] . '" /></div>';
} else {
    $item->title .= '<div class="thumb wrapme "><img src="wp-content/folder/default-image-url.jpg" alt="Deafult image description" /></div>';
}

编辑:

也许:

if (isset($image['url'] && isset($image['alt'])) {
    $item->title .= '<div class="thumb wrapme "><img src="' . $image['url'] . '" alt="' . $image['alt'] . '" /></div>';
} else {
    $item->title .= '<div class="thumb wrapme "><img src="wp-content/folder/default-image-url.jpg" alt="Deafult image description" /></div>';
}

会发生什么?

编辑:

然后只需设置标题而不是附加 maybe。

if (isset($image['url'] && isset($image['alt'])) {
    $item->title = '<div class="thumb wrapme "><img src="' . $image['url'] . '" alt="' . $image['alt'] . '" /></div>';
} else {
    $item->title = '<div class="thumb wrapme "><img src="wp-content/folder/default-image-url.jpg" alt="Deafult image description" /></div>';
}