【问题标题】:Replace text in a wordpress file with a hook用钩子替换 wordpress 文件中的文本
【发布时间】:2013-04-22 09:36:48
【问题描述】:

我正在尝试替换 woocommerce 文件中的一些文本,因此我需要在我的 functions.php 文件中创建一个挂钩,以便当 woocommerce 更新时,它不会通过恢复到原始代码来损害我的网站。

我正在尝试替换这部分代码:

<h3><?php the_title(); ?></h3>

用这个:

<div class="caption"><h5><?php the_title(); ?></h5>
  <div class="category-main-points"><?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
  </div>
</div>

这是原始文件中的部分代码:

<li <?php post_class( $classes ); ?>>

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

<a href="<?php the_permalink(); ?>">

    <?php
        /**
         * woocommerce_before_shop_loop_item_title hook
         *
         * @hooked woocommerce_show_product_loop_sale_flash - 10
         * @hooked woocommerce_template_loop_product_thumbnail - 10
         */
        do_action( 'woocommerce_before_shop_loop_item_title' );
    ?>

    <h3><?php the_title(); ?></h3>

    <?php
        /**
         * woocommerce_after_shop_loop_item_title hook
         *
         * @hooked woocommerce_template_loop_price - 10
         */
        do_action( 'woocommerce_after_shop_loop_item_title' );
    ?>

</a>

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

</li>

到目前为止,这是我的钩子:

<?php
// Add text to content-product-cat to help with styling
add_action('woocommerce_before_subcategory_title', 'woohook_before_subcategory_title');

function woohook_before_subcategory_title() {
ob_start();

}

add_action('woocommerce_after_subcategory_title', 'woohook_after_subcategory_title');

function woohook_after_subcategory_title() {
 $subcategory_title = ob_get_clean();

}
?>

有什么想法吗?

【问题讨论】:

    标签: php wordpress hook woocommerce


    【解决方案1】:

    Woocommerce 允许您覆盖其模板,只需将原始文件复制到 ./wp-content/themes/my-theme/woocommerce/ 并编辑即可!

    【讨论】:

    • 谢谢 diggy,我不知道。
    • 嗨,diggy,这似乎对很多文件都有效,但我刚刚编辑了 empty-cart.php 文件,但它不起作用。原始文件位置在 woocomerce/templates/cart,我已经
    • 嗨,diggy,这似乎对很多文件都有效,但我刚刚编辑了 empty-cart.php 文件,但它不起作用。原始文件位置在 woocomerce/templates/cart 中,如前所述,我已将一个新文件添加到我的 woocommerce 文件夹中,用于其他文件,如 content-product_cat.php 并且它们可以工作,但 empty-cart.php 没有,任何想法?
    • 我猜你必须尊重文件夹结构,所以试着把它放在./wp-content/themes/my-theme/woocommerce/cart/
    • 谢谢 diggy,我以为我已经这样做了,但我把它放在了模板/购物车而不是购物车
    猜你喜欢
    • 2015-09-10
    • 2021-07-01
    • 2021-04-04
    • 2013-05-30
    • 1970-01-01
    • 2018-01-08
    • 2011-08-26
    • 2019-04-06
    • 2011-02-26
    相关资源
    最近更新 更多