wordpress 禁用 Woocommerce 上的商店页面以保护类别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12716020/
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
Disable Shop page on Woocommerce to protect categories
提问by chifliiiii
Im trying to disable "shop" page in Woocommerce. Basically im creating a shop theme to sell prints and image downloads for a photographer.
我试图在 Woocommerce 中禁用“商店”页面。基本上我正在创建一个商店主题来为摄影师出售印刷品和图像下载。
Because i need to create private galleries i created a custom post type where i use the woocommerce category shortcode to show products and then i password protect the post type.
因为我需要创建私人画廊,所以我创建了一个自定义帖子类型,我使用 woocommerce 类别短代码来显示产品,然后我用密码保护帖子类型。
This is a workaround for password protecting the woocommerce categories (if someone knows a better one please explain).
这是密码保护 woocommerce 类别的解决方法(如果有人知道更好的请解释)。
The problem is that is someone goes to /shop they will all products, including the "protected ones". So i need to disable the shop page and i need to do it programmatically on my theme functions. Any thoughts?
问题是有人去/shop 他们会购买所有产品,包括“受保护的产品”。所以我需要禁用商店页面,我需要在我的主题功能上以编程方式执行此操作。有什么想法吗?
采纳答案by David Hobs
*Edit - Apparently the page setting I suggested below no longer works. If WooCommerce doesn't have a plugin setting to change it, I personally would use a wordpress redirect plugin like Redirection. This way you can automatically redirect them from the undesired shop page to whatever page displays your products. It avoids a 404 issue and keeps everything in tact. It also avoids editing template files which adds complications to non-developers.
*编辑 - 显然我在下面建议的页面设置不再有效。如果 WooCommerce 没有插件设置来更改它,我个人会使用像Redirection这样的 wordpress 重定向插件。通过这种方式,您可以自动将它们从不需要的商店页面重定向到显示您的产品的任何页面。它避免了 404 问题并保持一切正常。它还避免了对非开发人员增加复杂性的编辑模板文件。
Old Answer:
旧答案:
Have you tried Woo settings?
您是否尝试过 Woo 设置?
Admin area, left main menu, Woocommerce > Settings Click the pages tab.
管理区域,左侧主菜单,Woocommerce > 设置 单击页面选项卡。
Under Pages setup is "Shop Base Page", on the dropdown, there's a small "x" to right right. Click that to get rid of the page.
在页面设置下是“Shop Base Page”,在下拉菜单中,右侧有一个小“x”。单击它以摆脱该页面。
If there are links elsewhere that need to be fixed let me know and I'll find the hooks/filters to remedy it.
如果其他地方有需要修复的链接,请告诉我,我会找到挂钩/过滤器来修复它。
回答by Solomon Closson
To disable the shop page, copy over the archive-product.php
file from the /wp-content/plugins/woocommerce/templates/archive-product.php
and put in /wp-content/themes/{Your Theme}/woocommerce/archive-product.php
要禁用商店页面,请archive-product.php
从复制文件/wp-content/plugins/woocommerce/templates/archive-product.php
并放入/wp-content/themes/{Your Theme}/woocommerce/archive-product.php
Open up the file and overwrite everything in file with the following code below:
打开文件并使用以下代码覆盖文件中的所有内容:
<?php
global $wp_query;
$wp_query->set_404();
status_header(404);
get_template_part('404');
Save the file, and now your Shop page is gone and replaced with a 404 Page!
保存文件,现在您的商店页面不见了,取而代之的是 404 页面!
回答by Solomon Closson
Add this to functions:
将此添加到函数:
function woocommerce_disable_shop_page() {
global $post;
if (is_shop()):
global $wp_query;
$wp_query->set_404();
status_header(404);
endif;
}
add_action( 'wp', 'woocommerce_disable_shop_page' );
回答by Lewis
WooCommerce has a filter for the array that it uses to create the Product post type: woocommerce_register_post_type_product
.
WooCommerce对数组的过滤器,使用它来创建产品后类型:woocommerce_register_post_type_product
。
Rather changing the archive template to force it to redirect, you can completely remove the post type's archive, but changing the has_archive
attribute on the post type on creation.
而不是更改存档模板以强制其重定向,您可以完全删除帖子类型的存档,但has_archive
在创建时更改帖子类型的属性。
add_filter('woocommerce_register_post_type_product', function($post_type) {
$post_type['has_archive'] = false;
return $post_type;
});
You should then remove the shop page in the CMS by going to WooCommerce ? Settings ? Product ? Display, and clicking the “x” on the “Shop Page” option.
然后您应该通过转到WooCommerce删除 CMS 中的商店页面?设置?产品 ?显示,然后单击“商店页面”选项上的“x”。
You might need to flush the permalink cache, which you can do just by clicking the “Update” button in Settings ? Permalinks.
您可能需要刷新永久链接缓存,只需单击“设置”中的“更新”按钮即可完成?固定链接。
回答by Dudikowski
The last suggestion didn't work for me with WP 4.6.1 and WooCommerce 2.6.4. Hiding products in the Publish tab works for me.
最后一个建议对 WP 4.6.1 和 WooCommerce 2.6.4 不起作用。在“发布”选项卡中隐藏产品对我有用。
http://paperhedge.com/hide-products-from-displaying-in-shop-page-woocommerce/
http://paperhedge.com/hide-products-from-displaying-in-shop-page-woocommerce/
回答by Federico Schiocchet
To disable the default shoppage and leave the /shop/ slug freefor custom pages use this:
要禁用默认商店页面并为自定义页面保留 /shop/ slug,请使用以下命令:
function remove_woocommerce_default_shop( $args, $post_type ) {
if (class_exists('WooCommerce')) {
if ( $post_type == "product" ) {
$args['has_archive'] = true;
}
return $args;
}
}
add_filter('register_post_type_args', 'remove_woocommerce_default_shop', 20, 2);
回答by ceotoolsuite
template_redirect is the last hook before page render so in my use case I ask if the page being viewed is the "shop" page and if it is I redirect to (in my case) a pricing page.
template_redirect 是页面渲染之前的最后一个钩子,所以在我的用例中,我询问正在查看的页面是否是“商店”页面,如果是我重定向到(在我的情况下)定价页面。
function my__template_redirect(){
if(is_shop()){
wp_redirect(site_url() . '/pricing/', '302');
}
}
add_action('template_redirect', 'my__template_redirect');
回答by rgdesign
You need to hook a couple (or maybe more) things:
您需要挂钩一些(或更多)东西:
/* hide category from shop pages */
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 ), // Don't display products in the knives category on the shop page
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
Change, obviusly YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 for your cat or cats to hide from shop pages.
显然,更改您的类别 SLUG、您的类别 SLUG-2,以便您的猫或猫从商店页面中隐藏。
Then, you need to hide them from menues too, right?:
然后,您也需要将它们从菜单中隐藏,对吗?:
/* hide category from menues */
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if category and on the shop page (change it with is_woocommerce() if want to hide from all woo pages)
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
But i don′t think this solve 100% the thing, since then, what about search results?
但我不认为这能 100% 解决问题,从那时起,搜索结果呢?
回答by user3395368
Try this
尝试这个
- Create new page named "Shop"
- Go to "woocommerce" > "Settings" > "Product" > "Display tab"
- Select shop page named "Shop" then click save changes
- Back to "Pages" then delete "Shop" page (keep the page on trash, don't delete permanently)
- 创建名为“商店”的新页面
- 转到“woocommerce”>“设置”>“产品”>“显示选项卡”
- 选择名为“Shop”的商店页面,然后单击保存更改
- 返回“页面”然后删除“商店”页面(将页面保留在垃圾箱中,不要永久删除)
This method works fine for me
这种方法对我来说很好用