我将 woocommerce 设置为在进入商店页面时仅显示类别(而不是产品),并且我希望每个类别都有一个“查看更多”按钮及其相应的 URL,就像产品一样。
这是我的代码:
add_action( 'woocommerce_after_subcategory_title', 'view_category_button', 10 );
function view_category_button() {
$link = get_term_link( (int)$product_cat_id, 'product_cat' );
echo '<a href="' . $link . '"> View more </a>';
}
但是,代码无法显示按钮并中断了商店的加载。
这是我想要的图片:https://imgur.com/98arCfL
这是我得到的图片:https://imgur.com/xwLK6r0
代码中的主要问题是
$product_cat_id
未定义。另外,最好在缩略图和标题周围的现有链接之后显示按钮。
尝试以下方法:
add_action( 'woocommerce_after_subcategory_title', 'view_category_button', 20 );
function view_category_button( $category ) {
printf('<a class="button" href="%s">%s</a>', esc_url( get_term_link( $category, 'product_cat' ) ), esc_html__('View more') );
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。