Проблема с регистрацией меню - Что делать, когда другие решения не работают?


Я не могу зарегистрировать навигационные меню - и я не уверен, что еще можно попробовать, когда опубликованные решения не работают?

Я попробовал решение в этом посте: register_nav_menus() не будет регистрировать меню

И также следовали инструкциям в: http://codex.wordpress.org/Function_Reference/register_nav_menus и http://wptuts.org/how-to-add-menu-to-wordpress-theme/

Я не могу заставить это работать - я все еще получите это сообщение:

"Текущая тема изначально не поддерживает меню, но вы можете использовать виджет "Пользовательское меню", чтобы добавить любые меню, которые вы создадите здесь, на боковую панель темы".

В настоящее время в моем functions.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');

Мой шаблон навигации (header-top-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'); ?>

Весь functions.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');

Может ли кто-нибудь сказать мне, чего мне не хватает, или есть ли что-то неправильное в моем коде?

Заранее спасибо за любую вашу помощь или указания!

Author: Community, 2012-12-06

1 answers

В связи с этим комментарием:

Я вижу - у меня были вещи, вложенные неправильно в мой functions.php file - Я обнаружил, что вызов register_nav_menus() не должен быть вложен в функцию настройки темы (function hchw_setup())

Напротив, внутри обратного вызова, подключенного к after_setup_theme, находится точно подходящее место для вызовов 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');
 2
Author: Chip Bennett, 2012-12-06 21:12:05