将顶部菜单移到购物车下方并在标题中搜索

问题描述 投票:0回答:1

我创建了一个自定义顶级菜单,其中包含电子邮件/电话/社交联系详细信息

我还有一些产品搜索表单的自定义HTML和当前出现在联系人菜单上方的购物车下拉菜单...我需要切换它们,以便联系人详细信息位于顶部,我的搜索和卡片就在下面!这是情况的图片......

enter image description here

这是我当前的标题代码,你可以看到我有wp_nav_menu()函数那里添加顶级菜单...

<?php
/**
 * The header for our theme.
 *
 * Displays all of the <head> section and everything up till <div id="content">
 *
 * @package storefront
 */

?><!doctype html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">

<?php wp_head(); ?>
</head>

<body <?php body_class(); ?>>

<?php do_action( 'storefront_before_site' ); ?>

<div id="page" class="hfeed site">
    <?php do_action( 'storefront_before_header' ); ?>

    <header id="masthead" class="site-header" role="banner" style="<?php storefront_header_styles(); ?>">
        <div class="col-full">

            <?php wp_nav_menu( array( 'theme_location' => 'top-menu', 'menu_class' => 'top-menu') ); ?>

            <?php
            /**
             * Functions hooked into storefront_header action
             *
             * @hooked storefront_skip_links                       - 0
             * @hooked storefront_social_icons                     - 10
             * @hooked storefront_site_branding                    - 20
             * @hooked storefront_secondary_navigation             - 30
             * @hooked storefront_product_search                   - 40
             * @hooked storefront_primary_navigation_wrapper       - 42
             * @hooked storefront_primary_navigation               - 50
             * @hooked storefront_header_cart                      - 60
             * @hooked storefront_primary_navigation_wrapper_close - 68
             */
            do_action( 'storefront_header' ); ?>

        </div>
    </header><!-- #masthead -->

    <?php
    /**
     * Functions hooked in to storefront_before_content
     *
     * @hooked storefront_header_widget_region - 10
     */
    do_action( 'storefront_before_content' ); ?>

    <div id="content" class="site-content" tabindex="-1">
        <div class="col-full">

        <?php
        /**
         * Functions hooked in to storefront_content_top
         *
         * @hooked woocommerce_breadcrumb - 10
         */
        do_action( 'storefront_content_top' );

我的购物车和搜索代码需要在wp_nav_menu()函数下,但我不知道如何让它出现在那里(使用functions.php?)。

到目前为止,我的functions.php看起来像这样:

<?php

/**
 * register top menu
 */
function register_custom_menus()
{
   register_nav_menu('top-menu',__( 'Top Menu' ));
}
add_action( 'init', 'register_custom_menus' );

/**
 * remove cart widget from main menu
 */
function hom_remove_header_cart()
{
    remove_action( 'storefront_header', 'storefront_header_cart', 60 );
}
add_action( 'init', 'hom_remove_header_cart' );

/**
 * remove product search from header
 */
function hom_remove_product_search()
{
    remove_action( 'storefront_header', 'storefront_product_search', 40 );
}
add_action( 'init', 'hom_remove_product_search' );

/*
customise the mini_basket
*/
function storefront_top_cart()
{
    if (is_woocommerce_activated()) {
        if ( is_cart() ) {
            $class = 'current-menu-item';
        } else {
            $class = '';
        }
        ?>
            <div class="hom-cart-menu">
                <form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
                    <label class="screen-reader-text" for="woocommerce-product-search-field-<?php echo isset( $index ) ? absint( $index ) : 0; ?>"><?php esc_html_e( 'Search for:', 'woocommerce' ); ?></label>
                    <input type="search" id="woocommerce-product-search-field-<?php echo isset( $index ) ? absint( $index ) : 0; ?>" class="search-field" placeholder="<?php echo esc_attr__( 'Search products&hellip;', 'woocommerce' ); ?>" value="<?php echo get_search_query(); ?>" name="s" /><button type="submit" value="<?php echo esc_attr_x( 'Search', 'submit button', 'woocommerce' ); ?>">
                        <i class="fa fa-search"></i>
                    </button>
                    <input type="hidden" name="post_type" value="product" />
                </form>
                <ul class="site-header-cart menu top-cart">
                    <li class="<?php echo esc_attr( $class ); ?>">
                        <?php storefront_cart_top_link(); ?>
                    </li>
                    <?php the_widget( 'WC_Widget_Cart', 'title=' ); ?>
                </ul>
            </div>
        <?php
    }
}

function storefront_cart_top_link()
{
    ?>
        <a class="cart-contents" href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'storefront' ); ?>">
            <?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?>Â  || <span class="count"><?php echo wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'storefront' ), WC()->cart->get_cart_contents_count() ) );?></span>
        </a>
    <?php
}

/*
Add search to top navigation menu
*/
add_filter( 'wp_nav_menu_items', 'add_basket_box', 20, 2 );
function add_basket_box($items, $args)
{
    if ($args->theme_location == 'top-menu') {
        $items .= '<li>'.storefront_top_cart().'</li>';
    }
    return $items;
}

这是输出的HTML

<div class="col-full">
                <div class="hom-cart-menu">
                    <form role="search" method="get" class="woocommerce-product-search" action="http://localhost/client_projects/hom/site/">
                        <label class="screen-reader-text" for="woocommerce-product-search-field-0">Search for:</label>
                        <input id="woocommerce-product-search-field-0" class="search-field" placeholder="Search products…" value="" name="s" type="search"><button type="submit" value="Search">
                            <i class="fa fa-search"></i>
                        </button>
                        <input name="post_type" value="product" type="hidden">
                    </form>
                    <ul class="site-header-cart menu top-cart">
                        <li class="">
                            <a class="cart-contents" href="http://localhost/client_projects/hom/site/basket/" title="View your shopping basket">
                                <span class="amount">£0.00</span> <span class="count">0 items</span>
                            </a>
                        </li>
                        <div class="widget woocommerce widget_shopping_cart">
                            <div class="widget_shopping_cart_content">
                                <p class="woocommerce-mini-cart__empty-message">No products in the basket.</p>
                            </div>
                        </div>                
                    </ul>
                </div>
                <div class="menu-top-bar-menu-container">
                    <ul id="menu-top-bar-menu" class="top-menu">
                        <li id="menu-item-34" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-34">
                            <a href="tel:0800001066"><i class="fa fa-phone"></i> 01228 739713</a>
                        </li>
                        <li id="menu-item-35" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-35">
                            <a href="mailto:[email protected]"><i class="fa fa-envelope"></i> [email protected]</a>
                        </li>
                        <li></li>
                    </ul>
                </div>
            </div>

我不明白为什么或如何在我的顶级菜单上生成购物车和搜索代码!

我怎样才能重新订购它们?

php html wordpress
1个回答
1
投票

我有一段时间没有使用Wordpress,但我记得遇到这个问题并使用输出缓冲区修复它:

function storefront_top_cart()
{
    # Add the start
    ob_start();

    if (is_woocommerce_activated()) {
        if ( is_cart() ) {
            $class = 'current-menu-item';
        } else {
            $class = '';
        }
        ?>
            <div class="hom-cart-menu">
                <form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
                    <label class="screen-reader-text" for="woocommerce-product-search-field-<?php echo isset( $index ) ? absint( $index ) : 0; ?>"><?php esc_html_e( 'Search for:', 'woocommerce' ); ?></label>
                    <input type="search" id="woocommerce-product-search-field-<?php echo isset( $index ) ? absint( $index ) : 0; ?>" class="search-field" placeholder="<?php echo esc_attr__( 'Search products&hellip;', 'woocommerce' ); ?>" value="<?php echo get_search_query(); ?>" name="s" /><button type="submit" value="<?php echo esc_attr_x( 'Search', 'submit button', 'woocommerce' ); ?>">
                        <i class="fa fa-search"></i>
                    </button>
                    <input type="hidden" name="post_type" value="product" />
                </form>
                <ul class="site-header-cart menu top-cart">
                    <li class="<?php echo esc_attr( $class ); ?>">
                        <?php storefront_cart_top_link(); ?>
                    </li>
                    <?php the_widget( 'WC_Widget_Cart', 'title=' ); ?>
                </ul>
            </div>
        <?php
    }
    # Fetch the cache
    $data = ob_get_contents();
    # End and clear the buffer
    ob_end_clean();
    # Send data back for render
    return $data;
}

function storefront_cart_top_link()
{
    # Add the start
    ob_start();
    ?>
        <a class="cart-contents" href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'storefront' ); ?>">
            <?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?>Â  || <span class="count"><?php echo wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'storefront' ), WC()->cart->get_cart_contents_count() ) );?></span>
        </a>
    <?php
    # Fetch the cache
    $data = ob_get_contents();
    # End and clear the buffer
    ob_end_clean();
    # Send data back for render
    return $data;
}
© www.soinside.com 2019 - 2024. All rights reserved.