WP_bootstrap_Navwalker_issue

时间:2018-12-10 作者:ayushMaan

我在我的引导程序wordpress中放了一个navwalker,这给了我一个致命的错误

致命错误:未捕获错误:在:\\xampp\\htdocs\\wordpress\\WP content\\themes\\Akiveb\\header中找不到类“WP\\u Bootstrap\\u Navwalker”。php:29堆栈跟踪:#0 A:\\xampp\\htdocs\\wordpress\\wp includes\\template。php(688):require\\u once()#1 A:\\xampp\\htdocs\\wordpress\\wp includes\\template。php(647):加载\\u模板(\'A:\\xampp\\htdocs…\',true)\\2 A:\\xampp\\htdocs\\wordpress\\wp includes\\general模板。php(41):locate\\u template(Array,true)#3 A:\\xampp\\htdocs\\wordpress\\wp content\\themes\\Akiveb\\index。php(46):get\\u header()#4 A:\\xampp\\htdocs\\wordpress\\wp includes\\template loader。php(74):包括(\'A:\\xampp\\htdocs…\')\\5 A:\\xampp\\htdocs\\wordpress\\wp blog header。php(19):require\\u once(\'A:\\xampp\\htdocs…\')\\6 A:\\xampp\\htdocs\\wordpress\\index。php(17):在:\\xampp\\htdocs\\wordpress\\wp content\\themes\\Akiveb\\header中抛出require(\'A:\\xampp\\htdocs…\')\\7{main}。php第29行

这是我的标题。php

<head>
<!-- Bootstrap core CSS -->
<link href="<?php bloginfo("template_url")?>/css/bootstrap.css" 
rel="stylesheet">

<!-- Custom styles for this template -->
<link href="<?php bloginfo("stylesheet_url")?>" rel="stylesheet">
<?php wp_head(); ?>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
  <div class="container">
    <a class="navbar-brand col-8 col-md-3" rel="home" href="<?php echo 
     esc_url( home_url( \'/\' ) ); ?>" title="<?php echo esc_attr( 
     get_bloginfo( 
     \'name\', \'display\' ) ); ?>"> <img src="<?php echo 
     get_stylesheet_directory_uri() . \'/images/logo-site.png\'; ?>"
     alt="<?php bloginfo(\'name\'); ?>" /> </a>

    <button class="navbar-toggler" type="button" data-toggle="collapse" 
     data-target="#navbarResponsive" aria-controls="navbarResponsive" aria- 
     expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarResponsive">
      <ul class="navbar-nav ml-auto">
      <?php

      wp_nav_menu( array(
        \'theme_location\'  => \'primary\',
        \'depth\'           => 2, // 1 = no dropdowns, 2 = with dropdowns.
        \'container\'       => \'div\',
        \'container_class\' => \'collapse navbar-collapse\',
        \'container_id\'    => \'navbarResponsive\',
        \'menu_class\'      => \'navbar-nav mr-auto\',
        \'fallback_cb\'     => \'WP_Bootstrap_Navwalker::fallback\',
        \'walker\'          => new WP_Bootstrap_Navwalker(),
      ) );

      ?>
      </ul>
     </div>
   </div>
  </nav>
  </body>
这就是我的功能。php
function remove_admin_bar(){
if (!current_user_can(\'administrator\') && !is_admin()){
  show_admin_bar(\'false\');
}

}

if ( ! file_exists( get_template_directory() . \'/class-wp-bootstrap- 
navwalker.php\' ) ) {
// file does not exist... return an error.
return new WP_Error( \'class-wp-bootstrap-navwalker-missing\', __( \'It appears 
the class-wp-bootstrap-navwalker.php file may be missing.\', \'wp-bootstrap- 
navwalker\' ) );
} else {
// file exists... require it.
require_once get_template_directory() . \'/class-wp-bootstrap-navwalker.php\';
}


//Theme Support
function wpb_theme_setup(){
//nav Menus
register_nav_menus( array(
\'primary\' => __( \'Primary Menu\'),
) );
}


add_action(\'after_setup_theme\',\'wpb_theme_setup\');




function prefix_modify_nav_menu_args( $args ) {
return array_merge( $args, array(
\'walker\' => WP_Bootstrap_Navwalker(),
) );
}
add_filter( \'wp_nav_menu_args\', \'prefix_modify_nav_menu_args\' );
?>

1 个回复
SO网友:Remzi Cavdar

示例Bootstrap 4WP Bootstrap Navwalker

我建议您header.php 很简单,把你所有的逻辑都放进去functions.php

同时使用wp_enqueue_style 排队stylesheetswp_enqueue_script 排队JavaScript. 这样,您就不会破坏与其他插件的兼容性,并确保最佳实践。

参见关于使用的技术辩论WordPress Coding Standards: How to add crossorigin and integrity to wp_register_style? (Font Awesome 5)functions.php not adding css to website?

您需要使用after_setup_themewp_enqueue_scripts 挂钩。这些示例是使用Bootstrap 4制作的

示例标题。php

<?php
/**
 * The header for our theme
 *
 * This is the template that displays all of the <head> section and everything up until <div id="content">
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package Advanza-Direct
 */

?>
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
    <!-- Required meta tags -->
    <meta charset="<?php bloginfo( \'charset\' ); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="profile" href="https://gmpg.org/xfn/11">
    <!-- End required meta tags -->
    <!-- wp_head -->
    <?php wp_head(); ?>
    <!-- End wp_head -->
</head>
<body <?php body_class(); ?>>

<div id="page" class="site">
    <header id="masthead" class="site-header">
        <div id="navigation" class="main-navigation">
            <nav class="navbar navbar-expand-md navbar-dark container">
                <div class="site-branding">
                    <?php
                    // the_custom_logo();
                    wp_theme_name_prefix_custom_logo();
                    ?>
                </div><!-- .site-branding -->

                <button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
                  <span class="navbar-toggler-icon"></span>
                </button>

                <?php
                wp_nav_menu( array(
                    \'theme_location\'  => \'primary-menu\',
                    \'depth\'           => 2, // 1 = no dropdowns, 2 = with dropdowns.
                    \'container\'       => \'div\',
                    \'container_class\' => \'navbar-collapse collapse\',
                    \'container_id\'    => \'navbarCollapse\',
                    \'menu_class\'      => \'navbar-nav ml-auto\', // mr-auto = menu left, ml-auto = menu right.
                    \'fallback_cb\'     => \'WP_Bootstrap_Navwalker::fallback\',
                    \'walker\'          => new WP_Bootstrap_Navwalker(),
                ) );
                ?>
          </nav>
        </div><!-- #site-navigation -->
  </header><!-- #masthead -->

    <div id="content" class="site-content">
函数示例。php
// Register Custom Navigation Walker
if ( ! file_exists( get_template_directory() . \'/inc/class-wp-bootstrap-navwalker.php\' ) ) {
    // file does not exist... return an error.
    return new WP_Error( \'class-wp-bootstrap-navwalker-missing\', __( \'It appears the class-wp-bootstrap-navwalker.php file may be missing.\', \'wp-bootstrap-navwalker\' ) );
} else {
    // file exists... require it.
    require_once get_template_directory() . \'/inc/class-wp-bootstrap-navwalker.php\';
}


if ( ! function_exists( \'wp_theme_name_prefix_setup\' ) ) {
    /**
     * Sets up theme defaults and registers support for various WordPress features.
     *
     * Note that this function is hooked into the after_setup_theme hook, which
     * runs before the init hook. The init hook is too late for some features, such
     * as indicating support for post thumbnails.
     */
    function wp_theme_name_prefix_setup() {
        /*
         * Make theme available for translation.
         * Translations can be filed in the /languages/ directory.
         * If you\'re building a theme based on Advanza-Direct, use a find and replace
         * to change \'advanza-direct\' to the name of your theme in all the template files.
         */
        load_theme_textdomain( \'wp-theme-name-prefix\', get_template_directory() . \'/languages\' );

        // Add default posts and comments RSS feed links to head.
        add_theme_support( \'automatic-feed-links\' );

        /*
         * Let WordPress manage the document title.
         * By adding theme support, we declare that this theme does not use a
         * hard-coded <title> tag in the document head, and expect WordPress to
         * provide it for us.
         */
        add_theme_support( \'title-tag\' );

        /*
         * Enable support for Post Thumbnails on posts and pages.
         *
         * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
         */
        add_theme_support( \'post-thumbnails\' );

        // This theme uses wp_nav_menu() in one location.
        register_nav_menus( array(
            \'primary-menu\' => esc_html__( \'Primary menu\', \'wp-theme-name-prefix\' ),
        ) );

        /*
         * Switch default core markup for search form, comment form, and comments
         * to output valid HTML5.
         */
        add_theme_support( \'html5\', array(
            \'search-form\',
            \'comment-form\',
            \'comment-list\',
            \'gallery\',
            \'caption\',
        ) );

        // Set up the WordPress core custom background feature.
        add_theme_support( \'custom-background\', apply_filters( \'wp_theme_name_prefix_custom_background_args\', array(
            \'default-color\' => \'ffffff\',
            \'default-image\' => \'\',
        ) ) );

        // Add theme support for selective refresh for widgets.
        add_theme_support( \'customize-selective-refresh-widgets\' );

        /**
         * Add support for core custom logo.
         *
         * @link https://codex.wordpress.org/Theme_Logo
         */
        add_theme_support( \'custom-logo\', array(
            \'height\'      => 50,
            \'width\'       => 350,
            \'flex-width\'  => true,
            \'flex-height\' => true,
        ) );

        function wp_theme_name_prefix_custom_logo() {
            if ( function_exists( \'the_custom_logo\' ) ) {
                the_custom_logo();
            }
        }

        function change_logo_class($html) {
            $html = str_replace( \'custom-logo-link\', \'custom-logo-link navbar-brand\', $html );
            return $html;
        }
        add_filter( \'get_custom_logo\', \'change_logo_class\' );

    }
}
add_action( \'after_setup_theme\', \'wp_theme_name_prefix_setup\' );


/**
 * Enqueue scripts and styles.
 */
function wp_theme_name_prefix_scripts() {
    // Add Bootstrap and Font Awesome CSS
    wp_enqueue_style( \'bootstrap\', get_theme_file_uri( \'/assets/css/bootstrap.min.css\' ), array(), null );
    // wp_enqueue_style( \'fontawesome-style\', get_theme_file_uri( \'/assets/css/all.css\' ), array(), null );

    // Theme stylesheet
    wp_enqueue_style( \'wp-theme-name-prefix\', get_stylesheet_uri(), array(\'bootstrap\'), null );

    // Example add Google Fonts
    wp_enqueue_style(\'google_fonts\', \'https://fonts.googleapis.com/css?family=Poppins:300,500,700\', array(), null );

    // bootstrap bundle js (Popper.js included)
    wp_enqueue_script( \'bootstrap-bundle-js\', get_theme_file_uri( \'/assets/js/bootstrap.bundle.min.js\' ), array( \'jquery\' ), null, true );

    // explanation: https://wordpress.stackexchange.com/questions/211701/what-does-wp-embed-min-js-do-in-wordpress-4-4
    // https://kinsta.com/knowledgebase/disable-embeds-wordpress/
    wp_deregister_script( \'wp-embed\' );
    wp_dequeue_script( \'wp-embed\' );

    if ( is_singular() && comments_open() && get_option( \'thread_comments\' ) ) {
        wp_enqueue_script( \'comment-reply\' );
    }
}
add_action( \'wp_enqueue_scripts\', \'wp_theme_name_prefix_scripts\' );

相关推荐

如何创建4列和2列Twitter-Bootstrap相结合的WordPress循环?

我想根据4列(桌面视图)、2列(mobile view 768px)和1列(mobile view 425px)的组合显示帖子。我在下面找到了很棒的代码:<?php $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1; $args=array( \'post_type\' => \'post\',