类别项目不会显示在一行中

时间:2016-09-01 13:30:58

标签: css wordpress woocommerce

我正在为客户端完成自定义Storefront子主题。昨天,当我开始工作时,我注意到主页类别行项目显示在两行上。我没有对任何模板代码或CSS进行任何更改。无论我做什么,我都无法让它们显示在一行上。我已经移除浮动,放置浮动:向左移动,移除边距和填充,没有任何更改纠正类别项目显示的方式。

网站网址:https://test.onyour6designs.com

CSS

.page-template-template-homepage-php .site-main .columns-3 ul.products, 
.storefront-full-width-content .site-main .columns-3 ul.products {
    width: 100%;
    display: block;
}

.page-template-template-homepage-php .site-main .columns-3 ul.products li.product, 
.storefront-full-width-content .site-main .columns-3 ul.products li.product {
    display: inline-block;
    float: left;
    width: 29%;
    margin-right: 0;
}
.page-template-template-homepage-php .site-main .columns-3 ul.products li.product:last-child, 
.storefront-full-width-content .site-main .columns-3 ul.products li.product:last-child {
      margin-right: 0;
}

Other CSS:
storefront style.css
ol, ul {
    margin: 0 0 1.618em 3em;
    padding: 0;
}
* {
    box-sizing: border-box;
}

woocommerce.css
ul.products {
    margin-left: 0;
    clear: both;
}
ul.products:after, ul.products:before {
    content: "";
    display: table;
}
ul.products:after {
    clear: both;
}
ul.products:after, ul.products:before {
    content: "";
    display: table;
}

自定义PHP(在儿童主题中)

店面-模板的functions.php

if ( ! function_exists( 'storefront_oy6d_product_categories' ) ) {
/**
 * Display Product Categories
 * Hooked into the `homepage` action in the homepage template
 *
 * @since  1.0.0
 * @param array $args the product section args.
 * @return void
 */
function storefront_oy6d_product_categories( $args ) {

    if ( is_woocommerce_activated() ) {

        $args = apply_filters( 'storefront_oy6d_product_categories_args', array(
            'limit'             => 3,
            'columns'           => 3,
            'child_categories'  => 0,
            'orderby'           => 'name',
            'title'             => __( 'Shop by Category', 'storefront' ),
        ) );

        echo '<section class="storefront-oy6d-product-categories phone">';

        do_action( 'storefront_homepage_before_product_categories' );

        echo '<h2 class="section-title">' . wp_kses_post( $args['title'] ) . '</h2>';

        do_action( 'storefront_homepage_after_product_categories_title' );

        echo storefront_do_shortcode( 'product_categories', array(
            'number'  => intval( $args['limit'] ),
            'columns' => intval( $args['columns'] ),
            'orderby' => esc_attr( $args['orderby'] ),
            'parent'  => esc_attr( $args['child_categories'] ),
        ) );

        do_action( 'storefront_homepage_after_product_categories' );

        echo '</section>';
    }
  }
}

呈现HTML

<ul class="products">
    <li class="product-category product">
        <a href="https://test.onyour6designs.com/product-category/gun-holsters/">
            <div class="pso-wrap">
                <img src="//test.onyour6designs.com/wp-content/uploads/2016/09/custom-gun-holsters-420x350.jpg" alt="Holsters" width="420" height="535" /></a>      
                <div class="pso-title">
                    <a href="https://test.onyour6designs.com/product-category/gun-holsters/"><h3>Holsters <mark class="count">(21)</mark>       </h3> </a>      
                </div>
            </div>
    </li>
    <li class="product-category product first">
        <a href="https://test.onyour6designs.com/product-category/magazine-holsters/">  
            <div class="pso-wrap">
                <img src="//test.onyour6designs.com/wp-content/uploads/2014/09/OnYour6Designs_Ruger_SR22_Holster-5-420x535.jpg" alt="Mag Holsters" width="420" height="535" />
        </a>        
        <div class="pso-title"> 
            <a href="https://test.onyour6designs.com/product-category/magazine-holsters/">
                <h3>Mag Holsters <mark class="count">(13)</mark>        </h3>
            </a>        
        </div>
        </div>
    </li>
    <li class="product-category product last">
        <a href="https://test.onyour6designs.com/product-category/on-your-6-gear/"> 
            <div class="pso-wrap">
                <img src="//test.onyour6designs.com/wp-content/uploads/2015/06/Gear-420x535.png" alt="On Your 6 Gear" width="420" height="535" />
        </a>        
        <div class="pso-title"> 
            <a href="https://test.onyour6designs.com/product-category/on-your-6-gear/">
                <h3>On Your 6 Gear <mark class="count">(2)</mark>       </h3>

            </a>        
        </div>
        </div>
    </li>
</ul>

更新:我禁用了我的子主题样式表,但问题仍然存在。我切换到父主题,类别项目显示在一行中。我只有一个儿童主题样式表。

更新:彻底检查了自定义PHP函数的语法错误,但没有找到。

更新:我从子主题中删除了所有自定义函数和挂钩,但仍然存在此问题。它似乎可能与我的template-homepage.php文件有关,但我不确定为什么。我对它做的唯一改变是用我的钩子替换自定义店面模板功能。

1 个答案:

答案 0 :(得分:1)

在提出这样的问题时,同样要包含相关的HTML也很重要。我在这里提到了您提到的产品类别部分的HTML:

<ul class="products">
    <li class="product-category product">
        <a href="https://test.onyour6designs.com/product-category/gun-holsters/">
            <div class="pso-wrap">
                <img src="//test.onyour6designs.com/wp-content/uploads/2016/09/custom-gun-holsters-420x350.jpg" alt="Holsters" width="420" height="535" /></a>      
                <div class="pso-title">
                    <a href="https://test.onyour6designs.com/product-category/gun-holsters/"><h3>Holsters <mark class="count">(21)</mark>       </h3> </a>      
                </div>
            </div>
    </li>
    <li class="product-category product first">
        <a href="https://test.onyour6designs.com/product-category/magazine-holsters/">  
            <div class="pso-wrap">
                <img src="//test.onyour6designs.com/wp-content/uploads/2014/09/OnYour6Designs_Ruger_SR22_Holster-5-420x535.jpg" alt="Mag Holsters" width="420" height="535" />
        </a>        
        <div class="pso-title"> 
            <a href="https://test.onyour6designs.com/product-category/magazine-holsters/">
                <h3>Mag Holsters <mark class="count">(13)</mark>        </h3>
            </a>        
        </div>
        </div>
    </li>
    <li class="product-category product last">
        <a href="https://test.onyour6designs.com/product-category/on-your-6-gear/"> 
            <div class="pso-wrap">
                <img src="//test.onyour6designs.com/wp-content/uploads/2015/06/Gear-420x535.png" alt="On Your 6 Gear" width="420" height="535" />
        </a>        
        <div class="pso-title"> 
            <a href="https://test.onyour6designs.com/product-category/on-your-6-gear/">
                <h3>On Your 6 Gear <mark class="count">(2)</mark>       </h3>

            </a>        
        </div>
        </div>
    </li>
</ul>

这里有几件事情:

如果您检查输出,您会发现由于某种原因,第二个产品正在获得first类。

first的部分样式为clear: both !important;,强制它位于第一个产品之下,即float: left

我个人不喜欢这种情况下的花车,所以我建议调整.productfloat: none的样式,并保留display: inline-block

请注意,摆弄它时似乎适合我的样式是:

.page-template-template-homepage-php .site-main .columns-3 ul.products li.product, 
.storefront-full-width-content .site-main .columns-3 ul.products li.product {
    display: inline-block;
    float: none;
    width: 29%;
    margin-right: 0;
    vertical-align: top;
}

其他人可能会告诉你,使用flexbox是答案,我不会反对 - 这不是我的“去”方法。 (如果您预计有超过3个类别,并且类别项目可能具有不同高度的风险,那么flexbox 是正确的方法)。

作为旁注,你在样式表的第26行有这种风格:

* {
    box-sizing: border-box;
}

是一个非常“危险”的规则。我在网站上遇到了一个问题,需要花费数小时来解决这个问题。

我会强烈反对*规则。我唯一使用*的方法是将边距和填充重置为0.

最后 - 请注意您的商品li元素中的标记不正确。将打开a代码,然后会打开div代码,然后a代码在 div代码关闭之前关闭假设您使用的是HTML5 - 可以清理标记,如下所示:

    <li class="product-category product last">
        <a href="https://test.onyour6designs.com/product-category/on-your-6-gear/"> 
            <div class="pso-wrap">
                <img src="//test.onyour6designs.com/wp-content/uploads/2015/06/Gear-420x535.png" alt="On Your 6 Gear" width="420" height="535" />
                <div class="pso-title"> 
                    <h3>On Your 6 Gear <mark class="count">(2)</mark></h3>
                </div>
            </div>
        </a>        
    </li>

(如果您没有使用HTML5,那么在a标记周围包装div标记是不合法/正确的