php Woocommerce 商店页面自定义模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37407888/
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
Woocommerce shop page custom template
提问by Silver Ringvee
As I understand by default Woocommerce shop page uses product archive template. What I am looking for, is to use a custom template for shop page.
据我了解,Woocommerce 商店页面默认使用产品存档模板。我正在寻找的是为商店页面使用自定义模板。
Here is what I did:
这是我所做的:
- Create template "my-shop"
- Create page "My shop" -> choose template "my-shop"
- Choose "My shop" as Woocommerce shop page
- 创建模板“我的商店”
- 创建页面“我的商店”-> 选择模板“我的商店”
- 选择“我的商店”作为 Woocommerce 商店页面
But none of the changes I made to "my-shop" template are present on the shop page.
但是我对“my-shop”模板所做的任何更改都没有出现在商店页面上。
What am I missing here? I would not like to change product archive itself, just the shop page.
我在这里缺少什么?我不想改变产品档案本身,只是商店页面。
Is there a way to disable product archive from being a default for shop page?
有没有办法禁用产品存档作为商店页面的默认设置?
Thanks
谢谢
回答by Blue Grass
I know it's too late and you may have figured it out by now. In any case, the changes that you would like to make to the WooCommerce Shop Page need to be done in the archive-product.php
and it would be safer to create a child theme and do these changes. Making enhancements and customizations in a Child theme is best practice so that you can update the parent theme at any time and it won't affect your store.
我知道为时已晚,您现在可能已经想通了。在任何情况下,您想要对 WooCommerce 商店页面进行的更改都需要在 中完成,archive-product.php
创建子主题并进行这些更改会更安全。在子主题中进行增强和自定义是最佳做法,这样您就可以随时更新父主题而不会影响您的商店。
I hope this helps, for more information on how you can use WooCommerce short-codes to customize your store can be found here.
我希望这会有所帮助,有关如何使用 WooCommerce 短代码自定义商店的更多信息,请访问此处。
回答by Jon
To add to Silver Ringvee's answer - he used is_page
but that only works on wordpress pages. For woocommerce you need to use something like is_woocommerce()
. See Woocommerce conditional tags page.
添加到 Silver Ringvee 的答案 - 他使用过,is_page
但仅适用于 wordpress 页面。对于 woocommerce,您需要使用类似is_woocommerce()
. 请参阅Woocommerce 条件标签页面。
My example code uses the is_shop
conditional tag as that was the page you wanted to change. the code get_template_part( 'content', 'shop' );
will call the file content-shop.php in your theme root folder. This code is to be added at the top of wp-content\themes\*theme*\woocommerce\archive-product.php
that you can copy from wp-content\plugins\woocommerce\templates\archive-product.php
我的示例代码使用is_shop
条件标记,因为这是您要更改的页面。代码get_template_part( 'content', 'shop' );
将调用主题根文件夹中的文件 content-shop.php。此代码将添加在wp-content\themes\*theme*\woocommerce\archive-product.php
您可以从中复制的顶部wp-content\plugins\woocommerce\templates\archive-product.php
You can add it just before get_header( 'shop' );
line 23 in my file - and the entire page will be drawn from your template. If you want to keep the header of the shop page, then put this code after the get_header
code. Remember to include a footer in your file as well
您可以get_header( 'shop' );
在我的文件中的第 23 行之前添加它- 整个页面将从您的模板中绘制。如果你想保留店铺页面的头部,那么把这个代码放在代码之后get_header
。请记住在您的文件中也包含页脚
if (is_shop()) {
get_template_part( 'content', 'shop' );
} else {
#normal archive-product code here
}
回答by Silver Ringvee
The solution (not perfect) that I figured to work best, until someone finds a way to actually change the template from dashboard:
我认为最有效的解决方案(不完美),直到有人找到一种方法来实际更改仪表板中的模板:
Adding:
添加:
<?php
if (is_page( 'Page Title' ) ):
# Do your stuff
endif;
?>
to content-product.php
in my theme's woocommerce folder.
到content-product.php
我主题的 woocommerce 文件夹中。
回答by codingpuss
If you prefer to go with code, you can create a redirect from the original shop page to your page via wp_redirect.
如果您更喜欢使用代码,您可以通过 wp_redirect 创建从原始商店页面到您的页面的重定向。
add_action('template_redirect', 'bc_010101_redirect_woo_pages');
function bc_010101_redirect_woo_pages()
{
if (is_shop())
{
wp_redirect('your_shop_url_here');
exit;
}
}
More detailed tutorial can be found here
更详细的教程可以在这里找到
回答by anubhav
This is not possible to create custom template for shop page , just copy and paste woocommerce Templates into you theme folder and Try to work in content-product.php template .
这无法为商店页面创建自定义模板,只需将 woocommerce 模板复制并粘贴到您的主题文件夹中,然后尝试在 content-product.php 模板中工作。