wordpress - 在 WordPress 中添加菜单项

标签 wordpress menu menuitem

我正在使用 wp_nav_menu(),并且我想将搜索框添加为菜单的一部分。

我无法弄清楚,希望得到一些帮助:

我的代码:

        <?php 
        $args = array('theme_location' => 'primary', 'container' => false);
        wp_nav_menu( $args ); 
        ?>

现在我想在末尾或菜单

    添加一个额外的
  • 元素,而我在
  • 中想要的只是输出:

            <?php get_search_form();?>
    

    这可以做到吗?

    函数.php:

    add_action('init', 'register_top_menu');
    
    function register_top_menu() {
        register_nav_menu('primary', __('Top Menu', 'leeaenergy'));
    }
    
    
    add_filter('wp_nav_menu_items','add_search', 10, 2);
    
    function add_search($items, $args) {
    
        if( $args->theme_location == 'primary' )
        return $items . '<li>'.get_search_form().'</li>';
    }
    

最佳答案

是的,您需要使用过滤器。将以下代码添加到您的functions.php 文件中。将 theme_location 更改为为当前主题设置的任何位置:

add_filter( 'wp_nav_menu_items', 'add_search', 10, 2 );

function add_search( $items, $args ) {
    if ( $args->theme_location == 'primary' ) {
        return $items . '<li>' . get_search_form() . '</li>';
    }
}

关于wordpress - 在 WordPress 中添加菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9439877/

相关文章:

javascript - 如何根据用户在angularjs中的角色显示不同的菜单项?

php - "Your PHP installation appears to be missing the MySQL extension which is required by WordPress."

html - 链接到列表中单个元素的心脏/喜欢按钮

angular - 来自 Angular 5 中的 json 对象的动态嵌套 Material 菜单

html - CSS 大型下拉菜单

android - 方向改变后 MenuItem alpha 值丢失

css - 仅将图像调整到一定大小

html - 如何将 Jupyter Notebook 转换为适合 Wordpress 的 HTML

wpf - 解决方法在WPF中震荡工具栏中的菜单样式

Android:菜单项的自定义 View