在wordpress中,如何创建窗口小部件并添加到菜单栏中

时间:2016-12-09 08:20:41

标签: wordpress woocommerce widget dropdown

我在wordpress中是绿色的。

我创建了一个小部件调用let ay: [String:String] = ["key1":"verios", "key2":"fdsf"] var array:[Any] = ["apple", "banana", "dragonfruit"] array.insert(ay, at:2) print(array) //Result : ["apple", "banana", ["key1": "verios", "key2": "fdsf"], "dragonfruit"] ,并将其放在与我正在使用的主题相同的文件夹中。但为什么我无法在我的Appereance - >小部件中找到这个小部件?

另一方面,我的菜单栏没有任何小部件,我如何将这个小部件添加到菜单栏之中或之前?

非常感谢!

这是我的php代码:

woocommerce-dropdown-cart.php

我正在使用名为“Creativo 5.0”的主题,我添加了:

 <?php class Woocommerce_Dropdown_Cart extends WP_Widget {
    public function __construct() {
        parent::__construct(
            'woocommerce-dropdown-cart', // Base ID
            'Woocommerce Dropdown Cart', // Name
            array( 'description' => __( 'Woocommerce Dropdown Cart', 'jeffho' ), ) // Args
        );
    }

    public function widget( $args, $instance ) {
        global $post;
        extract( $args );
        echo $before_widget;
        global $woocommerce;
        global $qode_options_proya;
        $cart_holder_class = 'header_cart';    
        if (isset($qode_options_proya['woo_cart_type'])){
            $cart_type = $qode_options_proya['woo_cart_type'];
            switch ($cart_type) {
                case 'font-elegant':
                    $cart_holder_class = "header_cart cart_icon";
                    break;
                default:
                    $cart_holder_class = "header_cart";
                    break;
            }
        }
        ?>
        <div class="shopping_cart_outer">
        <div class="shopping_cart_inner">
        <div class="shopping_cart_header">
            <a class="<?php echo esc_attr($cart_holder_class);?>" href="<?php echo $woocommerce->cart->get_cart_url(); ?>"><span class="header_cart_span"><?php echo $woocommerce->cart->cart_contents_count; ?></span></a>
            <div class="shopping_cart_dropdown">
            <div class="shopping_cart_dropdown_inner">
                <?php
                    $cart_is_empty = sizeof( $woocommerce->cart->get_cart() ) <= 0;
                    $list_class = array( 'cart_list', 'product_list_widget' );
                ?>
                    <ul class="<?php echo implode(' ', $list_class); ?>">    
                        <?php if ( !$cart_is_empty ) : ?>    
                            <?php foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) :    
                                $_product = $cart_item['data'];    
                                // Only display if allowed
                                if ( ! $_product->exists() || $cart_item['quantity'] == 0 ) {
                                    continue;
                                }    
                                // Get price
                                $product_price = get_option( 'woocommerce_tax_display_cart' ) == 'excl' ? $_product->get_price_excluding_tax() : $_product->get_price_including_tax();

                                $product_price = apply_filters( 'woocommerce_cart_item_price_html', woocommerce_price( $product_price ), $cart_item, $cart_item_key );
                                ?>    
                                <li>
                                    <a itemprop="url" href="<?php echo get_permalink( $cart_item['product_id'] ); ?>">    
                                        <?php echo $_product->get_image(); ?>    
                                        <?php echo apply_filters('woocommerce_widget_cart_product_title', $_product->get_title(), $_product ); ?>    
                                    </a>    
                                    <?php echo $woocommerce->cart->get_item_data( $cart_item ); ?>    
                                    <?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s &times; %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
                                </li>    
                            <?php endforeach; ?>    
                        <?php else : ?>    
                            <li><?php _e( 'No products in the cart.', 'woocommerce' ); ?></li>    
                        <?php endif; ?>    
                    </ul>
                </div>
            <?php if ( sizeof( $woocommerce->cart->get_cart() ) <= 0 ) : ?>             
            <?php endif; ?>

                <a itemprop="url" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" class="qbutton white view-cart"><?php _e( 'Cart', 'woocommerce' ); ?> <i class="fa fa-shopping-cart"></i></a>    
                    <span class="total"><?php _e( 'Total', 'woocommerce' ); ?>:<span><?php echo $woocommerce->cart->get_cart_subtotal(); ?></span></span>


            <?php if ( sizeof( $woocommerce->cart->get_cart() ) <= 0 ) : ?>

            <?php endif; ?>
    </div>
</div>
        </div>
        </div>
    <?php
        echo $after_widget;
    }   

    public function update( $new_instance, $old_instance ) {
        $instance = array();    
        return $instance;
    }    
} 
add_action( 'widgets_init', create_function( '', 'register_widget( "Woocommerce_Dropdown_Cart" );' ) );
?>
<?php 
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;
    ob_start();
    ?>
    <span class="header_cart_span"><?php echo $woocommerce->cart->cart_contents_count; ?></span>
    <?php
        $fragments['span.header_cart_span'] = ob_get_clean();
        return $fragments;  
}
?>

进入函数creativo_widgets_init()

我想要的是将php小部件添加到菜单栏中。

0 个答案:

没有答案