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 <li>
.
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') {
$top_level++;
}
}
$index = round($top_level / 2) - 1;
return $index;
Any help would be greatly appreciated. Thanks.
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' ) {
$counter++;
{
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' ) {
$counter++;
}
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=""/>
</a>
</div>
EOD;
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.
Thanks!
Your function name might be used by other plugins or themes and may cause a problem.
I suggest you either change the function name or use function_exists within if else statement.
Here is the manual > http://php.net/manual/bg/function.function-exists.php
Quick suggestion: `
if(!function_exists('main'){
function main(){
$do_stuff = 'Currently doing';
return $do_stuff;
}
}
`
我使用主题Page Builder Framework,然后在自定义外观时可以选择一个中心菜单,其中徽标位于中间,并且菜单是分开的。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.