Javascript 如何在WooCommerce产品页面上添加和设置新的默认(活动)产品选项卡?
我正在WordPress平台下使用WooCommerce插件,并试图更改Javascript 如何在WooCommerce产品页面上添加和设置新的默认(活动)产品选项卡?,javascript,php,jquery,wordpress,woocommerce,Javascript,Php,Jquery,Wordpress,Woocommerce,我正在WordPress平台下使用WooCommerce插件,并试图更改single product.php页面中的默认选项卡 现在第二个选项卡处于活动状态,我想将第一个选项卡更改为默认的活动选项卡 我使用woocommerce\u product\u选项卡更改了优先级,如下所示: add_filter('woocommerce_product_tabs', function($tabs) { global $post, $product; // Access to the curr
single product.php
页面中的默认选项卡
现在第二个选项卡处于活动状态,我想将第一个选项卡更改为默认的活动选项卡
我使用woocommerce\u product\u选项卡更改了优先级,如下所示:
add_filter('woocommerce_product_tabs', function($tabs) {
global $post, $product; // Access to the current product or post
$custom_tab_title = get_field('props', $post->ID);
$tabs['reviews']['priority'] = 10;
if (!empty($custom_tab_title)) {
$tabs['awp-' . sanitize_title('props')] = [
'title' => 'אביזרים',
'callback' => 'awp_custom_woocommerce_tabs',
'priority' => 5
];
}
return $tabs;
});
我还尝试使用jQuery在类active
的基础上更改它,如下所示:
jQuery(document).ready(function($){
console.log( "ready!" );
jQuery('.reviews_tab').removeClass('active');
jQuery('#tab-title-awp-props').addClass('active');
});
到目前为止,一切都没起作用
如何更改WooCommerce单一产品页面中的默认活动选项卡?您使用的WooCommerce\u product\u选项卡
钩子是正确的,功能也是正确的。它在模板中使用:/woocommerce/single product/tabs/tabs.php
但是,缺少回调函数
您可以在此处找到完整的示例:
要将新产品选项卡设置为默认值,则需要使用以下(完整)代码:
props
假定是通过ACF(高级自定义字段)插件添加的自定义字段
该代码已经过测试,可以正常工作。将它添加到活动主题的functions.php中。可以通过触发一个点击事件来尝试:jQuery('tabtitle awp props')。trigger('click')代码>@David不幸没有工作。当我使用lighthouse完成页面加载过程时,我看到页面在标签真正加载之前就准备好了,所以它影响了javascript功能。嘿,谢谢你的回答。虽然有些东西还没找到,但我似乎无法让它工作。由于$custom\u tab\u title
返回希伯来语值,jquery返回错误,所以我将其改回props。但我仍然将第二个选项卡作为默认选项卡激活。这是为什么?我没有注意到URL中不知怎么有评论。。所以它总是让我转向评论。
// adds and sets a new default product tab on the product page
add_filter('woocommerce_product_tabs', 'add_new_default_product_tab' );
function add_new_default_product_tab( $tabs ) {
global $product;
// set the new priority to the "reviews" tab
$tabs['reviews']['priority'] = 10;
// gets the value to use as the title and slug of the new tab
$custom_tab_title = get_field( 'props', $product->get_id() );
// if the custom field is set, it adds the new tab
if ( ! empty($custom_tab_title) ) {
$tabs['awp-' . sanitize_title($custom_tab_title)] = array(
'title' => 'אביזרים',
'callback' => 'awp_custom_woocommerce_tabs',
'priority' => 5
);
}
return $tabs;
}
// create the content of the new tab
function awp_custom_woocommerce_tabs() {
// the new tab content
}