我试图禁用Woocommerce中的“商店”页面。基本上我正在创建一个商店主题,为摄影师出售印刷品和图像下载。
因为我需要创建私人画廊我创建了一个自定义的帖子类型,我使用woocommerce类别短代码来显示产品,然后我的密码保护帖子类型。
这是密码保护woocommerce类别的解决方法(如果有人知道更好的,请解释)。
问题是,有人去/购物他们将所有产品,包括“受保护的”。所以我需要禁用商店页面,我需要以编程方式对我的主题功能进行操作。有什么想法吗?
*编辑 - 显然我在下面建议的页面设置不再有效。如果WooCommerce没有插件设置来改变它,我个人会使用像Redirection这样的wordpress重定向插件。这样,您可以自动将它们从不受欢迎的商店页面重定向到显示您的产品的任何页面。它避免了404问题,并保持一切顺利。它还避免了编辑模板文件,这增加了非开发人员的复杂性。
旧答案:
你试过Woo设置吗?
管理区域,左侧主菜单,Woocommerce>设置单击页面选项卡。
在页面设置下是“商店基页”,在下拉列表中,右侧有一个小的“x”。单击它以删除该页面。
如果其他地方有链接需要修复,请告诉我,我会找到钩子/过滤器来解决它。
我不知道为什么这么难找到网上的答案,为什么没有人能想到这个。但无论你选择与否,这都是正确的答案。
要禁用商店页面,请从archive-product.php
复制/wp-content/plugins/woocommerce/templates/archive-product.php
文件并放入/wp-content/themes/{Your Theme}/woocommerce/archive-product.php
打开文件并使用以下代码覆盖文件中的所有内容:
<?php
global $wp_query;
$wp_query->set_404();
status_header(404);
get_template_part('404');
?>
保存文件,现在您的商店页面已经消失,现在是404页面! TADA!
现在,所有那些说它无法完成的人,只是从菜单中删除它,以及所有那些疯狂的functions.php
行动解决方案不起作用的人以及所有那些人说要删除商店页面从Woocommerce-> Settings区域,现在可以吃自己的话,因为这些方法都不起作用,如果他们在回答这个问题之前花了5分钟自己测试一下,他们就会知道这个!
将此添加到函数:
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' );
WooCommerce有一个过滤器,用于创建Product post类型的数组:woocommerce_register_post_type_product
。
而是更改存档模板以强制其重定向,您可以完全删除帖子类型的存档,但在创建时更改帖子类型的has_archive
属性。
add_filter('woocommerce_register_post_type_product', function($post_type) {
$post_type['has_archive'] = false;
return $post_type;
});
然后,您应该通过转到WooCommerce»设置»产品»显示,然后单击“商店页面”选项中的“x”来删除CMS中的商店页面。
您可能需要刷新永久链接缓存,只需单击“设置”»“永久链接”中的“更新”按钮即可。
WP 4.6.1和WooCommerce 2.6.4对我的最后一个建议不起作用。在“发布”选项卡中隐藏产品对我有用。
http://paperhedge.com/hide-products-from-displaying-in-shop-page-woocommerce/
你需要挂钩一些(或者更多)的东西:
/* 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' );
}
改变,显然你是类别 - 塞,你是 - CATEGORY-SLUG-2为你的猫或猫隐藏在商店页面。
然后,你也需要将它们隐藏起来,对吧?:
/* 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;
}
但我不认为这解决了100%的问题,从那时起,搜索结果怎么样?
要禁用默认商店页面并将/ 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);
试试这个
这种方法对我来说很好