/*
Theme Name: Lightning Child
Theme URI:
Template: lightning
Description:
Author:
Tags:
Version: 0.6.1
*/

<?php
/**
 * メニューのラベル下に説明（description）を表示（親子共通・中央揃えはCSSで）
 */
add_filter('walker_nav_menu_start_el', function ($item_output, $item, $depth, $args) {
    // 説明が空なら何もしない
    $desc = isset($item->description) ? trim($item->description) : '';
    if ($desc === '') {
        return $item_output;
    }

    // 出力用HTML
    $desc_html  = '<span class="menu-desc">' . esc_html($desc) . '</span>';

    // まずは <a>...</a> の“中”に差し込む（タイトルを menu-title で包む）
    if (preg_match('#(<a[^>]*>)(.*?)(</a>)#is', $item_output)) {
        $item_output = preg_replace(
            '#(<a[^>]*>)(.*?)(</a>)#is',
            '$1' . '<span class="menu-title">$2</span>' . $desc_html . '$3',
            $item_output,
            1
        );
    } else {
        // テーマが特殊で <a> を持たない場合 → アンカーの直後に追加
        $item_output .= $desc_html;
    }

    // 目印クラス
    $item_output = str_replace('menu-item ', 'menu-item has-desc ', $item_output);

    return $item_output;
}, 10, 4);





