标题大小写WordPress菜单项

时间:2017-03-08 作者:NewJenk

我正在尝试将所有菜单项转换为标题框;这是我用来将输出更改为标题大小写的函数:

function titleCase($title) {

    $smallwordsarray = array( \'of\',\'a\',\'the\',\'and\',\'an\',\'or\',\'nor\',\'but\',\'is\',\'if\',\'then\',\'else\',\'when\', \'at\',\'from\',\'by\',\'on\',\'off\',\'for\',\'in\',\'to\',\'into\',\'with\' );

    // Split the string into separate words
    $words = explode(\' \', $title);

    foreach ($words as $key => $word) {

    // If this word is the first, or it\'s not one of our small words, capitalise it
    // with ucwords().
    if ($key == 0 or !in_array($word, $smallwordsarray))
        $words[$key] = ucwords($word);
    }

    // Join the words back into a string
    $newtitle = implode(\' \', $words);

    return $newtitle;

}
}

例如,如果我想更改标题输出,我可以这样做:

echo titleCase(get_the_title());
问题是我不知道如何将上述函数应用于wp\\u nav\\u menu()输出:

<?php wp_nav_menu( array( \'theme_location\' => \'primary\', \'menu_id\' => \'primary-menu\' ) ); ?>
非常感谢您的帮助。

1 个回复
SO网友:Svartbaard

看看下面的内容是否对你有用,我昨天发布了相关内容,以回答今天讨论的另一个问题。另请参见法典中关于继承Walker\\u Nav\\u菜单的要点。随后,您可以修改菜单的输出并使用您的功能。

法典中的示例:

<?php
class Walker_Quickstart_Menu extends Walker {

    // Tell Walker where to inherit it\'s parent and id values
    var $db_fields = array(
        \'parent\' => \'menu_item_parent\', 
        \'id\'     => \'db_id\' 
    );

    /**
     * At the start of each element, output a <li> and <a> tag structure.
     * 
     * Note: Menu objects include url and title properties, so we will use those.
     */
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $output .= sprintf( "\\n<li><a href=\'%s\'%s>%s</a></li>\\n",
            $item->url,
            ( $item->object_id === get_the_ID() ) ? \' class="current"\' : \'\',
            $item->title
        );
    }

}
以及

<ul>
    <?php
    wp_nav_menu(array(
        \'menu\'    => 2, //menu id
        \'walker\'  => new Walker_Quickstart_Menu() //use our custom walker
    ));
    ?>
</ul>
参考号:https://codex.wordpress.org/Class_Reference/Walker

相关推荐

如何将Java脚本添加到Custom-Page.php模板?

如何将javascript添加到自定义页面。php模板?如何使从w3schools ajax教程获得的以下javascript在自定义页面上工作。php模板?任何帮助都将不胜感激。工作javascript包含在以下HTML中:<!DOCTYPE html> <html> <style> table,th,td { border : 1px solid black; border-collapse: collapse;&#x