0

我试图在引导下拉列表的第一个(或)<li>之前添加一个新的,这将是下拉列表的标题。我知道 $output 应该有一个条件,但是我应该只在第一个下拉列表之前输出什么?<li><div><li>

到目前为止的代码(内部start_el()函数)-

if($depth == 1) {

        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';

        $item_output = $args->before;


        // The output to be filtered with condition
        $item_output .= '<div class="dropdown-item dropdown-title">DROPDOWN TITLE</div>';


        $item_output .= '<a class="dropdown-item" '. $attributes .'>';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;

}

输出在每个<li>. 截屏-

在此处输入图像描述

4

1 回答 1

0

如果那是在start_lvl方法中,那么我认为没有任何论据可以帮助您。但是您可以添加一个静态变量,并将其设置为false一旦处理了第一项,如果您愿意..

可能不是最干净的解决方案,但如果您没有找到另一个简单的解决方案,那么它可能会为您解决问题。

static $isDropdownTitleRendered = false; // Static variable so the value stays remembered during the request
if ($depth == 1) {
        // First part of your code
        // ...

        // The output to be filtered with condition
        if (!$isDropdownTitleRendered) {
                $item_output .= '<div class="dropdown-item dropdown-title">DROPDOWN TITLE</div>';
                $isDropdownTitleRendered = true;
        }

        // ...
        // Rest of your code
}
于 2020-05-01T19:07:01.563 回答