2016-04-14 8 views
0

EDIT:特定のメニュー項目をパディングするのではなく、メニューにロゴを直接挿入するようにパラメータを調整しました。パディング方法は、中央のメニューを中心から外れやすくします。この挿入方法は、この問題を解決するはずです。Wordpressテーマの中のメニュー項目としてロゴを挿入したい

私はテーマに取り組んでおり、ロゴによって分割されたメニューを作成したいと考えています。私は2つのメニューを作成することができたことを理解していますが、私はこれを可能な限りユーザーにとって合理化したいと考えています。私は既にアイテムの数を取得して、私が望むメニューアイテムをターゲットにすることができましたが、<li>にクラス「pad-item」を追加するためにfunctions.phpファイルを使用する方法がわかりません。

ここで指定したアイテムを見つけてターゲットにする必要があります。しかし、それはすべてトップレベルのアイテムのインデックス番号です。

$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; 

ご協力いただければ幸いです。ありがとう。

答えて

1

私は問題を把握することができました。他の誰かが同じ回答を探していた場合に備えて解決策を投稿したかったのです。

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; 
} 

私が何かを逃したのか、それとも別のやり方で行うことができたら教えてください。

ありがとうございます!

0

あなたの関数名は他のプラグインやテーマで使用されている可能性があり、問題が発生する可能性があります。

私は、関数名を変更するか、if else文の中でfunction_existsを使うことをお勧めします。 `

if(!function_exists('main'){ 
    function main(){ 
     $do_stuff = 'Currently doing'; 
     return $do_stuff; 
    } 
} 

`

:ここ

は>http://php.net/manual/bg/function.function-exists.php

クイック提案マニュアルです

関連する問題