php Woo commerce:在循环外编辑产品类别页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18640315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Woo commerce: editing product-category page outside the loop
提问by nickstaw
I have built a custom Wordpress theme and am using Woo Commerce for the shopping cart plugin. Most of it has integrated well (I have added hooks for the theme in functions.php and have copied Woo commerce template pages into woocommerce sub-folder in theme.) However I am trying to add a wrapper () outside of the loop on the product category pages and can't for the life of me work out what I need to edit in order to make it work. I need to do this in order to re-position the products via CSS. I could apply the css to but this is not specific enough for me
我已经构建了一个自定义的 Wordpress 主题,并且正在使用 Woo Commerce 作为购物车插件。其中大部分已经很好地集成(我在functions.php中为主题添加了钩子,并将Woo commerce模板页面复制到主题中的woocommerce子文件夹中。)但是我试图在循环之外添加一个包装器()产品类别页面,我一生都无法弄清楚我需要编辑什么才能使其工作。我需要这样做才能通过 CSS 重新定位产品。我可以将 css 应用到,但这对我来说不够具体
So far I have:
到目前为止,我有:
- Experimented with content-product_cat.php and content-product.php (but both operate within the loop.
- Experimented with a number of other template pages
- I have added the tags to the wrapper-start.php and wrapper-end.php pages. This works for the other pages but does not show in product-cateogry
- 对 content-product_cat.php 和 content-product.php 进行了试验(但两者都在循环内运行。
- 尝试了许多其他模板页面
- 我已将标签添加到 wrapper-start.php 和 wrapper-end.php 页面。这适用于其他页面,但不会显示在产品类别中
I have looked online and on Wordpress site but it does not offer any help on the matter. Any help would be greatly appreciated!
我已经在网上和 Wordpress 网站上查看过,但它没有提供任何有关此事的帮助。任何帮助将不胜感激!
Kind Regards Nick
亲切的问候尼克
回答by Tim
For small changes like this, there is no need to override entire files. Try using hooks provided by WooCommerce first. You can do this in your themes functions.php. These functions output a custom wrapper inside your top page wrapper.
对于这样的小改动,不需要覆盖整个文件。首先尝试使用 WooCommerce 提供的钩子。您可以在您的主题functions.php 中执行此操作。这些函数在首页包装器中输出一个自定义包装器。
function start_wrapper_here() { // Start wrapper
echo '<div class="custom-wrapper">';
}
add_action( 'woocommerce_before_main_content', 'start_wrapper_here', 20 );
function end_wrapper_here() { // End wrapper
echo '</div>';
}
add_action( 'woocommerce_after_main_content', 'end_wrapper_here', 20 );
If you want to add unique wrappers on all WooCommerce pages, you can do this by adding conditional statements to the function above. Visit this page on the WooCommerce sitefor more conditional tags.
如果您想在所有 WooCommerce 页面上添加唯一的包装器,您可以通过向上述函数添加条件语句来实现。访问 WooCommerce 网站上的此页面以获取更多条件标签。
function start_wrapper_here() { // Start wrapper
if (is_shop()) { // Wrapper for the shop page
echo '<div class="shop-wrapper">';
} elseif (is_product_category()) { // Wrapper for a product category page
echo '<div class="product-category-wrapper">';
} elseif (is_product()) { // Wrapper for a single product page
echo '<div class="product-wrapper">';
}
}
add_action( 'woocommerce_before_main_content', 'start_wrapper_here', 20 );
Don't forget closing them all again.
不要忘记再次关闭它们。
function end_wrapper_here() { // End wrapper
if (is_shop()) {
echo '</div>';
} elseif (is_product_category()) {
echo '</div>';
} elseif (is_product()) {
echo '</div>';
}
}
add_action( 'woocommerce_after_main_content', 'end_wrapper_here', 20 );
If you wan't to change the actual top page wrapper (default: <div id="container">
), you should edit a WooCommerce function for that. This function would normally call the wrapper-start.php
and wrapper-end.php
files using get_template_part()
. We now override this function and echo our own wrapper.
如果您不想更改实际的首页包装器(默认值<div id="container">
:),您应该为此编辑 WooCommerce 函数。该功能通常会调用wrapper-start.php
和wrapper-end.php
使用文件get_template_part()
。我们现在覆盖这个函数并回显我们自己的包装器。
function woocommerce_output_content_wrapper() { // Start wrapper for all the page contents
echo '<div class="custom-wrapper">';
}
add_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 20 );
function woocommerce_output_content_wrapper_end() { // End wrapper for all the page contents
echo '</div>';
}
add_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 20 );
回答by nickstaw
Just found the answer - I needed to modify archive-product.php
刚刚找到答案 - 我需要修改 archive-product.php
回答by Manish
Do one thing, create a folder in your custom theme root and name it 'woocommerce'. Now you can copy templates from the WooCommerce plugin into your custom theme woocommerce folder and override any WooCommerce template file. You can find WooCommerce template files in plugins/woocommerce/template/
做一件事,在您的自定义主题根目录中创建一个文件夹并将其命名为“woocommerce”。现在,您可以将 WooCommerce 插件中的模板复制到您的自定义主题 woocommerce 文件夹中,并覆盖任何 WooCommerce 模板文件。您可以在以下位置找到 WooCommerce 模板文件plugins/woocommerce/template/
Hope this will help you.
希望这会帮助你。