首页 > 解决方案 > 如果内容为空,则使用 php 隐藏选项卡

问题描述

链接到登台站点

链接到产品页面 第一个选项卡通过简码获取描述,第二个选项卡通过简码获取简短描述。我想要它,以便如果简短描述为空,则该选项卡将被删除。

标签是这个在此处输入图像描述

我将此代码与 jquery 脚本一起使用,但似乎无法使其正常工作

  // Remove tehnicki karakteristiki
function remove_tehnicki_karakteristiki() {
    if ( !is_singular(['product']) ){ // only on product page
    global $post, $product;
    $short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt ); // get short desc
    if (empty($short_description) ) {
        echo '<script>
        jQuery(#elementor-tab-title-1562).hide();
        </script>';
    } 
}
}

标签: javascriptphpjquerywordpresswoocommerce

解决方案


更新:

function remove_tehnicki_karakteristiki() {
    if ( !is_singular(['product']) ){ // only on product page
        global $post, $product;
        $short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt ); // get short desc

        echo "
        <script>
            jQuery(function ($) {
                $('.elementor-toggle .elementor-tab-content').filter(function () { 
                    let element = ($(this).find('.elementor-section-wrap').length == 0 ? $(this) : $(this).find('.elementor-section-wrap'))
                    if (!new RegExp('([A-z0-9])', 'gm').test(element.html())) element.closest('.elementor-toggle-item').hide();
                })
            });
    </script>";

    }
}

推荐阅读