admin管理员组文章数量:1122832
I like this solution from Ahmad M, a lot, but how do I add images in addition to titles, instead of replacing titles? (Sorry, I'm still pretty new to PHP and Stackexchange).
Pulling Featured Images in to a WordPress Menu
I mostly figured it out. I'm using Ahmad M's filter but needed to add ".$menu_object->title" to the thumbnail so now it looks like:
$menu_object->title = has_post_thumbnail($menu_object->object_id) ? get_the_post_thumbnail($menu_object->object_id, 'full') . $menu_object->title . : $menu_object->title;
Now my challenge is that I only want to add it to third level menu items (child of a child of a parent).
I like this solution from Ahmad M, a lot, but how do I add images in addition to titles, instead of replacing titles? (Sorry, I'm still pretty new to PHP and Stackexchange).
Pulling Featured Images in to a WordPress Menu
I mostly figured it out. I'm using Ahmad M's filter but needed to add ".$menu_object->title" to the thumbnail so now it looks like:
$menu_object->title = has_post_thumbnail($menu_object->object_id) ? get_the_post_thumbnail($menu_object->object_id, 'full') . $menu_object->title . : $menu_object->title;
Now my challenge is that I only want to add it to third level menu items (child of a child of a parent).
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Apr 10, 2017 at 13:12 red5red5 1011 silver badge2 bronze badges 5- I understand that you want to show featured image with each of your menu link. Is it correct? – BlueSuiter Commented Apr 10, 2017 at 13:28
- Yes. Ahma M's solution works really well, but I needed to modify it. I figured out how to do that, but now I need to make it work only third level menu items. – red5 Commented Apr 10, 2017 at 14:58
- Only for third level menus? – BlueSuiter Commented Apr 11, 2017 at 4:39
- Yes. I would like to understand how to set a parameter for any given level, but for my current project, I need third level menu items. – red5 Commented Apr 21, 2017 at 16:10
- can you please share the html you are going to use for same.. – BlueSuiter Commented Apr 21, 2017 at 18:12
1 Answer
Reset to default 0The following code will get you the featured image for 3rd level of your navbar.
You, have to pass the nav-menu id within wp_get_nav_menu_items()
. It will get you the menu list (For more details).
<ul>
<?php
$navMenu = wp_get_nav_menu_items(9); /*/Pass Nav Menu_id or Name*/
$previousMenuParent = $level = 0;
foreach ($navMenu as $menu) {
if($menu->menu_item_parent == 0)
{
$level = 0;
echo '<li><a href="'. $menu->url .'">'. $menu->title .'</a>';
}
elseif($menu->menu_item_parent != '' && $menu->menu_item_parent != $previousMenuParent)
{
$level++;
echo '<ul class="submenu">';
echo '<li><a href="'. $menu->url .'">'. $menu->title .'</a>';
$previousMenuParent = $menu->menu_item_parent;
}
elseif($previousMenuParent == $menu->menu_item_parent)
{
echo '</li><li><a href="'. $menu->url .'">';
if($level == 3)
{
echo get_the_post_thumbnail($menu->ID);
}
echo $menu->title .'</a>';
}
}
?>
</li>
</ul>
本文标签: menusAdd Featured Image and Title to wpnavmenu items
版权声明:本文标题:menus - Add Featured Image and Title to wp_nav_menu items 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287628a1927922.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论