【问题标题】:How can I change the product <h2> in Woocommerce Storefront to <h3>?如何将 Woocommerce Storefront 中的产品 <h2> 更改为 <h3>?
【发布时间】:2021-03-02 03:45:25
【问题描述】:

我正在将子主题用于 Woocommerce 的店面主题。在首页,它正在将特色产品的产品名称渲染为h2s。我想将这些更改为 h3s(SEO 原因)。

h2s 使用这个类:woocommerce-loop-product__title。我搜索了 Storefront 主题,发现该类的唯一地方是 CSS。

我查看了这个文件:/storefront/inc/storefront-template-functions.php 并找到了这个代码:

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

    if ( storefront_is_woocommerce_activated() ) {

        $args = apply_filters( 'storefront_featured_products_args', array(
            'limit'      => 4,
            'columns'    => 4,
            'orderby'    => 'date',
            'order'      => 'desc',
            'visibility' => 'featured',
            'title'      => __( 'We Recommend', 'storefront' ),
        ) );

        $shortcode_content = storefront_do_shortcode( 'products', apply_filters( 'storefront_featured_products_shortcode_args', array(
            'per_page'   => intval( $args['limit'] ),
            'columns'    => intval( $args['columns'] ),
            'orderby'    => esc_attr( $args['orderby'] ),
            'order'      => esc_attr( $args['order'] ),
            'visibility' => esc_attr( $args['visibility'] ),
        ) ) );

        /**
         * Only display the section if the shortcode returns products
         */
        if ( false !== strpos( $shortcode_content, 'product' ) ) {

            echo '<section class="storefront-product-section storefront-featured-products" aria-label="' . esc_attr__( 'Featured Products', 'storefront' ) . '">';

            do_action( 'storefront_homepage_before_featured_products' );

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

            do_action( 'storefront_homepage_after_featured_products_title' );

            echo $shortcode_content;

            do_action( 'storefront_homepage_after_featured_products' );

            echo '</section>';

        }
    }
}

}

我认为这会运行特色产品部分。

我尝试更改此代码:

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

到:

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

我将 /inc/ 文件夹上传到我的子目录,并且没有任何更改。我尝试将 storefront-template-functions.php 文件上传到原始店面主题的 /inc/ 文件中。同样,什么也没发生。

我被难住了。我真的认为我有正确的代码将这些更改为 h3s。知道我需要做什么才能将特色产品 h2s 更改为 h3s 吗?

【问题讨论】:

  • 确定没有改吗?有两种可能性。要么你编辑了错误的代码部分(可能有一些其他代码来生成 h2),要么它实际上被改变了,但是一些 css 使 h3 看起来像 h2。您可以右键单击并view source code 确定。
  • 我确实使用了浏览器的检查功能来确认它们仍然呈现为 h2s
  • 请注意:如果您的 JavaScript 更改了一些 HTML 标签,browser's inspect element 将不会显示原始标签。这就是为什么我建议view source。不过,我很高兴您自己找到了解决方案 :)
  • @goFrendiAsgard - 感谢您的提示。我没有意识到这一点。

标签: php wordpress woocommerce


【解决方案1】:

好吧,事实证明我看错了地方,做错了事。我需要在我的子主题中的 functions.php 文件中添加一些内容才能使其正常工作。这是我添加的内容:

remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
add_action('woocommerce_shop_loop_item_title', 'abChangeProductsTitle', 10 );
function abChangeProductsTitle() {
echo '<h3 class="woocommerce-loop-product__title">' . get_the_title() . '</h3>';
}

我从这个答案中得到了这段代码的基础: When changing woocommerce title hook the first item doesn't change

【讨论】:

  • 很好,但要小心 html 类,不是“woocommerce-loop-product_title”而是“woocommerce-loop-product__title”
  • @WebMat 很好。感谢您纠正我。
【解决方案2】:

可能有很多东西。首先想到的是呈现缓存网页的页面缓存系统。因此,如果有这样的系统,请禁用它。然后尝试通过单击 CTRL+F5 以强制新鲜方式查看页面。

如果这不起作用,接下来的事情可能是 CSS h2 和 h3 标记具有相同的值。检查您的 CSS 并查看是否有 h1、h2、h3、h4 声明,并查看它是否已被声明为组。如果是删除 h3 并根据您的需要设置样式。

【讨论】:

  • 它仍然呈现为 h2s。我检查了源代码。我在 Wordpress 中没有缓存插件。我刚刚尝试清除我的缓存,它仍然呈现为 h2s。
  • 啊,我明白了那个改变做了什么。它将特色部分的 h2 更改为 h3。所以,我想我需要更多地寻找正确的代码......
  • 好的,如果元素确实更改为 h3 并且没有任何区别,那么我绝对认为您的 CSS 文件已将您的 h*s 全部分组。 :)
猜你喜欢
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
相关资源
最近更新 更多