注册菜单的问题-当其他解决方案不起作用时该怎么办?

时间:2012-12-06 作者:ByteMyPixel

我无法注册导航菜单,而且我不知道当发布的解决方案不起作用时,还能尝试什么?

我在这篇文章中尝试过这个解决方案:register_nav_menus() won't register menus

并遵循以下说明:http://codex.wordpress.org/Function_Reference/register_nav_menushttp://wptuts.org/how-to-add-menu-to-wordpress-theme/

我无法让它工作-我仍然收到以下消息:

当前主题本身不支持菜单,但您可以使用“自定义菜单”小部件将您在此处创建的任何菜单添加到主题的侧栏中

当前在我的功能中。php文件(保持简单-但不起作用):

register_nav_menu(\'primary_navigation\', \'Primary Navigation\');
add_action(\'after_setup_theme\', \'hchw_setup\');
这对我也不起作用(hchw是我的主题名):

register_nav_menus(array(
    \'primary_navigation\' => __(\'Primary Navigation\', \'hchw\'),
    \'sub_navigation\' => __(\'Sub Navigation\', \'hchw\'),
));
add_action(\'after_setup_theme\', \'hchw_setup\');
我的导航模板(标题顶部的navbar.php):

if (has_nav_menu(\'primary_navigation\')) {
    wp_nav_menu(array(\'theme_location\' => \'primary_navigation\', \'menu_class\' => \'nav\'));
}
其中包括:<?php get_template_part(\'templates/header-top-navbar\'); ?>

整个功能。php文件:

// Use \'static\' instead of \'wp-content\'
function rename_wp_content($path) {
  return str_replace(\'wp-content\', \'static\', $path);
}
add_filter(\'template_directory_uri\', \'rename_wp_content\');

// Is this an auth page?
function is_auth_page() {
  return in_array($GLOBALS[\'pagenow\'], array(\'wp-login.php\', \'wp-register.php\'));
}

// Change login logo
function login_css() {
  wp_enqueue_style(\'login_css\', get_template_directory_uri() . \'/css/login.css\');
}
add_action(\'login_head\', \'login_css\');

if (!defined(\'__DIR__\')) { define(\'__DIR__\', dirname(__FILE__)); }

require_once locate_template(\'/lib/utils.php\');           // Utility functions
require_once locate_template(\'/lib/config.php\');          // Configuration and constants
require_once locate_template(\'/lib/activation.php\');      // Theme activation
require_once locate_template(\'/lib/cleanup.php\');         // Cleanup
require_once locate_template(\'/lib/htaccess.php\');        // Rewrites for assets, H5BP .htaccess
require_once locate_template(\'/lib/widgets.php\');         // Sidebars and widgets
require_once locate_template(\'/lib/scripts.php\');         // Scripts and stylesheets
require_once locate_template(\'/lib/post-types.php\');      // Custom post types
require_once locate_template(\'/lib/metaboxes.php\');       // Custom metaboxes
require_once locate_template(\'/lib/custom.php\');          // Custom functions
//require_once locate_template(\'/lib/wp-admin-menu-classes.php\'); // Load the Admin Menu Classes


/** 
* Load the Options Panel
**/
if ( !function_exists( \'optionsframework_init\' ) ) {
    define( \'OPTIONS_FRAMEWORK_DIRECTORY\', get_bloginfo(\'template_directory\') . \'/admin/\' );
    require_once dirname( __FILE__ ) . \'/admin/options-framework.php\';
}

function hchw_setup() {

// Make theme available for translation
load_theme_textdomain(\'hchw\', get_template_directory() . \'/lang\');

// Register wp_nav_menu() menus (http://codex.wordpress.org/Function_Reference/register_nav_menus)
//register_nav_menus(array(
//    \'primary_navigation\' => __(\'Primary Navigation\', \'hchw\'),
//    \'sub_navigation\' => __(\'Sub Navigation\', \'hchw\'),
//));


register_nav_menu(\'primary_navigation\', \'Primary Navigation\');
add_action(\'after_setup_theme\', \'hchw_setup\');


// Add post formats (http://codex.wordpress.org/Post_Formats)
// add_theme_support(\'post-formats\', array(\'aside\', \'gallery\', \'link\', \'image\', \'quote\', \'status\', \'video\', \'audio\', \'chat\'));

// Tell the TinyMCE editor to use a custom stylesheet
add_editor_style(\'assets/css/editor-style.css\');

/**
 * Custom images size
**/  
// Add post thumbnails (http://codex.wordpress.org/Post_Thumbnails)
// This is then pulled through to your theme useing the_post_thumbnail(\'custombig\'); 
add_theme_support(\'post-thumbnails\');
  set_post_thumbnail_size(150, 150, false);
  add_image_size(\'category-thumb\', 300, 9999); // 300px wide (and unlimited height)
  add_image_size(\'postfull\', 504, 334); // Blog Post Full-Size
  add_image_size(\'customfeatins\', 248, 165, true); //hp featured inset
  add_image_size(\'customfeatblg\', 290, 192, true); //int featured inset
  add_image_size(\'customfeed\', 136, 90, true); //feed thumbnails
  add_image_size(\'customparade\', 176, 98, true); //logo parade

}

// Remove the link and \'Powered by WordPress\' from the login page
function login_header_unlink() {
  return null;
}
add_filter(\'login_headerurl\', \'login_header_unlink\');
add_filter(\'login_headertitle\', \'login_header_unlink\');


/**
 * Add class to body tag if has sidebar
**/
function wpfme_has_sidebar($classes) {
    if (is_active_sidebar(\'sidebar\')) {
        // add \'class-name\' to the $classes array
        $classes[] = \'has_sidebar\';
    }
    // return the $classes array
    return $classes;
}
add_filter(\'body_class\',\'wpfme_has_sidebar\');
谁能告诉我我缺少了什么,或者我的代码是否有错误?

提前感谢您的任何帮助或指导!

1 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

关于这一评论:

我明白了-我的functions.php file - 我发现register_nav_menus() 调用不应嵌套在主题设置函数中(function hchw_setup())

相反,在连接到after_setup_themeexactly the right place 对于呼叫register_nav_menus().

您的问题是语法错误。您已放置add_action() 在试图挂接的回调中调用,因此,回调永远不会添加到挂接中:

function hchw_setup() {

// Make theme available for translation
load_theme_textdomain(\'hchw\', get_template_directory() . \'/lang\');

// Register wp_nav_menu() menus (http://codex.wordpress.org/Function_Reference/register_nav_menus)
//register_nav_menus(array(
//    \'primary_navigation\' => __(\'Primary Navigation\', \'hchw\'),
//    \'sub_navigation\' => __(\'Sub Navigation\', \'hchw\'),
//));


register_nav_menu(\'primary_navigation\', \'Primary Navigation\');
add_action(\'after_setup_theme\', \'hchw_setup\');


// Add post formats (http://codex.wordpress.org/Post_Formats)
// add_theme_support(\'post-formats\', array(\'aside\', \'gallery\', \'link\', \'image\', \'quote\', \'status\', \'video\', \'audio\', \'chat\'));

// Tell the TinyMCE editor to use a custom stylesheet
add_editor_style(\'assets/css/editor-style.css\');

/**
 * Custom images size
**/  
// Add post thumbnails (http://codex.wordpress.org/Post_Thumbnails)
// This is then pulled through to your theme useing the_post_thumbnail(\'custombig\'); 
add_theme_support(\'post-thumbnails\');
  set_post_thumbnail_size(150, 150, false);
  add_image_size(\'category-thumb\', 300, 9999); // 300px wide (and unlimited height)
  add_image_size(\'postfull\', 504, 334); // Blog Post Full-Size
  add_image_size(\'customfeatins\', 248, 165, true); //hp featured inset
  add_image_size(\'customfeatblg\', 290, 192, true); //int featured inset
  add_image_size(\'customfeed\', 136, 90, true); //feed thumbnails
  add_image_size(\'customparade\', 176, 98, true); //logo parade

}
注意这是如何在hchw_setup() 作用

add_action(\'after_setup_theme\', \'hchw_setup\');
您需要将其移动到功能外部:

function hchw_setup() {

// Make theme available for translation
load_theme_textdomain(\'hchw\', get_template_directory() . \'/lang\');

// Register wp_nav_menu() menus (http://codex.wordpress.org/Function_Reference/register_nav_menus)
//register_nav_menus(array(
//    \'primary_navigation\' => __(\'Primary Navigation\', \'hchw\'),
//    \'sub_navigation\' => __(\'Sub Navigation\', \'hchw\'),
//));


register_nav_menu(\'primary_navigation\', \'Primary Navigation\');


// Add post formats (http://codex.wordpress.org/Post_Formats)
// add_theme_support(\'post-formats\', array(\'aside\', \'gallery\', \'link\', \'image\', \'quote\', \'status\', \'video\', \'audio\', \'chat\'));

// Tell the TinyMCE editor to use a custom stylesheet
add_editor_style(\'assets/css/editor-style.css\');

/**
 * Custom images size
**/  
// Add post thumbnails (http://codex.wordpress.org/Post_Thumbnails)
// This is then pulled through to your theme useing the_post_thumbnail(\'custombig\'); 
add_theme_support(\'post-thumbnails\');
  set_post_thumbnail_size(150, 150, false);
  add_image_size(\'category-thumb\', 300, 9999); // 300px wide (and unlimited height)
  add_image_size(\'postfull\', 504, 334); // Blog Post Full-Size
  add_image_size(\'customfeatins\', 248, 165, true); //hp featured inset
  add_image_size(\'customfeatblg\', 290, 192, true); //int featured inset
  add_image_size(\'customfeed\', 136, 90, true); //feed thumbnails
  add_image_size(\'customparade\', 176, 98, true); //logo parade

}
// Move me outside the function
add_action(\'after_setup_theme\', \'hchw_setup\');

结束

相关推荐

Wordpress Custom Menus Issue

在我的模板中,我使用这样的调用来输出一些自定义菜单:<?php wp_nav_menu(array(\'container_class\' => \'secondary-navigation\', \'theme_location\' => \'secondary\')); ?> 自从升级到WordPress 3.1.4后,我就可以获得完整的页面列表,而不是自定义菜单我看到修复程序(通过谷歌)说我应该添加以下内容来修复此问题:\'fallback_cb\' => f