Php 子菜单上具有不同输出的Walker

Php 子菜单上具有不同输出的Walker,php,wordpress,wp-nav-walker,Php,Wordpress,Wp Nav Walker,我的主菜单中有两个不同的子菜单。我不希望walker在两个子菜单上都有相同的输出,因为我将在每个子菜单按钮上实现两个不同的功能 如何获得两种不同的输出,以便在第一个子菜单上输出例如First Button,在第二个子菜单上输出例如Second Button menu.php <?php wp_nav_menu(array( 'menu' => 'Main Navigation', 'container' => false,

我的主菜单中有两个不同的子菜单。我不希望walker在两个子菜单上都有相同的输出,因为我将在每个子菜单按钮上实现两个不同的功能

如何获得两种不同的输出,以便在第一个子菜单上输出例如
First Button
,在第二个子菜单上输出例如
Second Button

menu.php

<?php wp_nav_menu(array(
        'menu'       => 'Main Navigation',
        'container'  => false,
        'items_wrap' => '<ul class="main-menu">%3$s</ul>',
        'walker'         => new m2o_walker_nav_menu()
));

如果我理解正确,您希望输出与菜单项深度相关的不同按钮。我想用最简单的方法,您可以检查
$depth
变量并更改按钮的文本

快速解决方案:

public function start_lvl(&$output, $depth = 0, $args = array()) {
    $indent = str_repeat("\t", $depth);
    $buttontext = 'First Button';

    if ($depth == 2) {
        $buttontext = 'Second Button';
    }

    if ($depth == 3) {
        $buttontext = 'Third Button';
    }

    if ($depth == 4) {
        $buttontext = 'Fourth Button';
    }

    $output .= "\n$indent <button>$buttontext</button><ul class=\"sub-menu\">\n";
}
public function start_lvl(&$output, $depth = 0, $args = array()) {
    $indent = str_repeat("\t", $depth);
    $buttondata = self::getButtonData($depth);

    $output .= "\n" . $indent . "<button onClick='" . $buttondata['action'] . "'>" . $buttondata['text'] . "</button><ul class=\"sub-menu\">\n";
}

public static function getButtonData($depth) {

    $buttons = [
        [
            'text' => 'First Button',
            'action' => 'myFirstFunction'
        ],
        [
            'text' => 'Second Button',
            'action' => 'mySecondFunction'
        ],
        [
            'text' => 'Third Button',
            'action' => 'myThirdFunction'
        ]
    ];

    return $buttons[$depth];
}
public function start\u lvl(&$output,$depth=0,$args=array()){
$indent=str\u repeat(“\t”,$depth);
$buttontext=‘第一个按钮’;
如果($depth==2){
$buttontext=‘第二个按钮’;
}
如果($depth==3){
$buttontext=‘第三个按钮’;
}
如果($depth==4){
$buttontext=‘第四个按钮’;
}
$output.=“\n$indent$buttontext
    \n”; }
您可以使用更多变量(例如onClick操作)来扩展它。或者更简洁的方法是在m2o_walker_nav_菜单中创建一个新方法,根据按钮的深度返回表示按钮的变量

清洁剂解决方案:

public function start_lvl(&$output, $depth = 0, $args = array()) {
    $indent = str_repeat("\t", $depth);
    $buttontext = 'First Button';

    if ($depth == 2) {
        $buttontext = 'Second Button';
    }

    if ($depth == 3) {
        $buttontext = 'Third Button';
    }

    if ($depth == 4) {
        $buttontext = 'Fourth Button';
    }

    $output .= "\n$indent <button>$buttontext</button><ul class=\"sub-menu\">\n";
}
public function start_lvl(&$output, $depth = 0, $args = array()) {
    $indent = str_repeat("\t", $depth);
    $buttondata = self::getButtonData($depth);

    $output .= "\n" . $indent . "<button onClick='" . $buttondata['action'] . "'>" . $buttondata['text'] . "</button><ul class=\"sub-menu\">\n";
}

public static function getButtonData($depth) {

    $buttons = [
        [
            'text' => 'First Button',
            'action' => 'myFirstFunction'
        ],
        [
            'text' => 'Second Button',
            'action' => 'mySecondFunction'
        ],
        [
            'text' => 'Third Button',
            'action' => 'myThirdFunction'
        ]
    ];

    return $buttons[$depth];
}
public function start\u lvl(&$output,$depth=0,$args=array()){
$indent=str\u repeat(“\t”,$depth);
$buttondata=self::getButtonData($depth);
$output.=“\n”。$indent。”“$buttondata['text']。”
    \n”; } 公共静态函数getButtonData($depth){ $buttons=[ [ “文本”=>“第一个按钮”, “操作”=>“myFirstFunction” ], [ “文本”=>“第二个按钮”, 'action'=>'mySecondFunction' ], [ “文本”=>“第三个按钮”, “操作”=>“myThirdFunction” ] ]; 返回$按钮[$depth]; }
请记住这是未经测试的,因为我没有安装wordpress


我希望这会有所帮助。

如果我理解正确,您希望输出与菜单项深度相关的不同按钮。我想用最简单的方法,您可以检查
$depth
变量并更改按钮的文本

快速解决方案:

public function start_lvl(&$output, $depth = 0, $args = array()) {
    $indent = str_repeat("\t", $depth);
    $buttontext = 'First Button';

    if ($depth == 2) {
        $buttontext = 'Second Button';
    }

    if ($depth == 3) {
        $buttontext = 'Third Button';
    }

    if ($depth == 4) {
        $buttontext = 'Fourth Button';
    }

    $output .= "\n$indent <button>$buttontext</button><ul class=\"sub-menu\">\n";
}
public function start_lvl(&$output, $depth = 0, $args = array()) {
    $indent = str_repeat("\t", $depth);
    $buttondata = self::getButtonData($depth);

    $output .= "\n" . $indent . "<button onClick='" . $buttondata['action'] . "'>" . $buttondata['text'] . "</button><ul class=\"sub-menu\">\n";
}

public static function getButtonData($depth) {

    $buttons = [
        [
            'text' => 'First Button',
            'action' => 'myFirstFunction'
        ],
        [
            'text' => 'Second Button',
            'action' => 'mySecondFunction'
        ],
        [
            'text' => 'Third Button',
            'action' => 'myThirdFunction'
        ]
    ];

    return $buttons[$depth];
}
public function start\u lvl(&$output,$depth=0,$args=array()){
$indent=str\u repeat(“\t”,$depth);
$buttontext=‘第一个按钮’;
如果($depth==2){
$buttontext=‘第二个按钮’;
}
如果($depth==3){
$buttontext=‘第三个按钮’;
}
如果($depth==4){
$buttontext=‘第四个按钮’;
}
$output.=“\n$indent$buttontext
    \n”; }
您可以使用更多变量(例如onClick操作)来扩展它。或者更简洁的方法是在m2o_walker_nav_菜单中创建一个新方法,根据按钮的深度返回表示按钮的变量

清洁剂解决方案:

public function start_lvl(&$output, $depth = 0, $args = array()) {
    $indent = str_repeat("\t", $depth);
    $buttontext = 'First Button';

    if ($depth == 2) {
        $buttontext = 'Second Button';
    }

    if ($depth == 3) {
        $buttontext = 'Third Button';
    }

    if ($depth == 4) {
        $buttontext = 'Fourth Button';
    }

    $output .= "\n$indent <button>$buttontext</button><ul class=\"sub-menu\">\n";
}
public function start_lvl(&$output, $depth = 0, $args = array()) {
    $indent = str_repeat("\t", $depth);
    $buttondata = self::getButtonData($depth);

    $output .= "\n" . $indent . "<button onClick='" . $buttondata['action'] . "'>" . $buttondata['text'] . "</button><ul class=\"sub-menu\">\n";
}

public static function getButtonData($depth) {

    $buttons = [
        [
            'text' => 'First Button',
            'action' => 'myFirstFunction'
        ],
        [
            'text' => 'Second Button',
            'action' => 'mySecondFunction'
        ],
        [
            'text' => 'Third Button',
            'action' => 'myThirdFunction'
        ]
    ];

    return $buttons[$depth];
}
public function start\u lvl(&$output,$depth=0,$args=array()){
$indent=str\u repeat(“\t”,$depth);
$buttondata=self::getButtonData($depth);
$output.=“\n”。$indent。”“$buttondata['text']。”
    \n”; } 公共静态函数getButtonData($depth){ $buttons=[ [ “文本”=>“第一个按钮”, “操作”=>“myFirstFunction” ], [ “文本”=>“第二个按钮”, 'action'=>'mySecondFunction' ], [ “文本”=>“第三个按钮”, “操作”=>“myThirdFunction” ] ]; 返回$按钮[$depth]; }
请记住这是未经测试的,因为我没有安装wordpress


我希望这能帮上忙。

关于您的快速解决方案。我也想过使用深度,但显然他们使用相同的深度,所以两者都是“第一个按钮”。在你的cleaner解决方案中,我得到了一个语法错误,我无法清除<代码>语法错误,意外的“”(T_封装的_和_空格),应为“-”或标识符(T_字符串)或变量(T_变量)或数字(T_NUM_字符串)
我在更简洁的解决方案中犯了两个错误:1。我忘了把关键字
function
放在上面。2.start_lvl()的输出以错误的方式转义。我编辑了我的答案。请再试一次。清洁溶液现在运行。但是,它们似乎有相同的深度,所以同一个按钮出现两次。您是否尝试在wp_nav_menu()中指定导航的深度?试着把它设置为4或其他。也许wp_nav_menu()中有一个bug,当您相应地设置depth选项时,$depth将被正确设置。在您的快速解决方案中。我也想过使用深度,但显然他们使用相同的深度,所以两者都是“第一个按钮”。在你的cleaner解决方案中,我得到了一个语法错误,我无法清除<代码>语法错误,意外的“”(T_封装的_和_空格),应为“-”或标识符(T_字符串)或变量(T_变量)或数字(T_NUM_字符串)我在更简洁的解决方案中犯了两个错误:1。我忘了把关键字
function
放在上面。2.start_lvl()的输出以错误的方式转义。我编辑了我的答案。请再试一次。清洁溶液现在运行。但是,它们似乎有相同的深度,所以同一个按钮出现两次。您是否尝试在wp_nav_menu()中指定导航的深度?试着把它设置为4或其他。也许wp_nav_menu()中有一个bug,当您相应地设置depth选项时,$depth将被正确设置。