wordpress - Woocommerce:当类别仅包含一项时如何直接跳转到产品页面

标签 wordpress woocommerce

我有一个顶部菜单,可以显示产品类别。 在本例中,是我正在销售的应用程序的名称。

单击此菜单项时,它会显示类别的内容 - 正如它应该做的那样。

但是,由于该类别仅包含一项,因此我想直接跳转到产品页面,而不是显示包含一项的类别页面。

这里是相关页面的链接:boutique.zimrahapp.com/categorie-produit/app/

我无法找到可以调整输出或进行重定向的 Hook 或模板。

这种事情已经做过了吗?

最佳答案

上面的代码对我来说不起作用。我有一个可行的解决方案,但它会重定向所有单个存档结果。就我而言,我还想重定向单个标签。

/* Redirect if there is only one product in the category or tag, or anywhere... */

function redirect_to_single_post(){
global $wp_query;
if( is_archive() && $wp_query->post_count == 1 ){
the_post();
$post_url = get_permalink();
wp_safe_redirect($post_url , 302 );
 exit;
}
} 
add_action('template_redirect', 'redirect_to_single_post');

我在这里问过同样的问题:Woocommerce: How to automatically redirect to the single product if there is only one product on a category page?

关于wordpress - Woocommerce:当类别仅包含一项时如何直接跳转到产品页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35012094/

相关文章:

php - 如何在wordpress的查询中动态添加帖子数

wordpress - 如何检测并纠正博客上的这些错误?

php - 在 Woocommerce 编辑订单页面中显示可编辑的自定义字段值

javascript - 如何仅将搜索表单添加到商店页面和类别页面?

javascript - 如何修复 google places api initMap 不是函数?

html - 在 Tablepress (Wordpress) 中垂直对齐文本/图像

javascript - WooCommerce 移动变体描述

php - 在 WooCommerce 管理员手动订单上添加产品重量和尺寸作为订单项元数据

php - 结帐字段 - 使 billing_address_2 高于 billing_address_1

javascript - WordPress - 将模板文件夹图像添加到 JavaScript