【问题标题】:Category items won't display on a single row类别项目不会显示在单行上
【发布时间】:2016-09-01 23:11:39
【问题描述】:

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

网站网址: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(在子主题中)

店面模板功能.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 文件有关,但我不知道为什么。我对其所做的唯一更改是将我的钩子替换为自定义店面模板函数。

【问题讨论】:

    标签: css wordpress woocommerce


    【解决方案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

    我个人不喜欢这种情况下的浮动,所以我建议将.product 的样式调整为float: 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 标签周围是不合法/不正确的)

    【讨论】:

    • 嗨,cale_b--感谢您的意见!我同意盒子大小规则;但是,这是一种 woocommerce CSS 样式。我可以尝试覆盖它,看看会发生什么。感谢您包含呈现的 HTML——我太专注于这个问题,我忘了添加它:)。
    • cale_b:我尝试了 float:none 和 width:30%,但之前没有用。我会再试一次。可能是语法错误:)
    • cale_b:有效!我只需要再次调整其他样式。非常感谢您的帮助!我绞尽脑汁想弄清楚这个问题:)
    • 不客气。很高兴它帮助/解决了问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2022-06-29
    相关资源
    最近更新 更多