【问题标题】:Custom shortcode to display in hover悬停时显示的自定义简码
【发布时间】:2021-07-13 17:34:01
【问题描述】:

我想在我的产品(类别页面)上显示一个自定义属性作为悬停,但我遇到了困难。在我看来,最简单的方法是创建一个简码,但我无法克服它...... 这就是我所在的地方。 这个,使用 [product_description id = '22'] 工作正常,但我需要自动检索 id。

function display_product_description( $atts ){
    $atts = shortcode_atts( array(
        'id' =>  get_the_id(),
    ), $atts, 'product_description' );

    global $product;

    if ( ! is_a( $product, 'WC_Product') )
        $product = wc_get_product($atts['id']);
    
    return $product->get_attribute( 'Profil aromatique' );
}
add_shortcode( 'product_description', 'display_product_description' );

这段代码也很好用,但它显示在我的产品下方,我无法让它悬停。

function woocommerce_after_shop_loop_item_title_short_description2() {
    global $product;

    if ( ! $product->get_attribute( 'Profil aromatique' ) ) return;
    ?>
    <div itemprop="description" id="hovertest" class="test" >
        <?php echo apply_filters( 'woocommerce_short_description', $product->get_attribute( 'Profil aromatique' ) ) ?>
    </div>
    <?php
}
add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_after_shop_loop_item_title_short_description2', 5);

所以,我想混音...

function display_profil_description(){
global $product;
    if ( ! $product->get_attribute( 'Profil aromatique' ) ){ return '';}
    else{
    return apply_filters( 'woocommerce_short_description', $product->get_attribute( 'Profil aromatique' ) );
    }
}
add_shortcode( 'display_profil_description', 'display_profil_description' );

我想使用它 [display_profil_description] 并根据我展示它的产品即时找到 ID。我这样显示:

.woocommerce .et_overlay:before {
    left: 0;
    margin-left: 0;
  margin-top:-50px !important;
    content: '[display_profil_description]';
    /*** YOUR TEXT ***/
    font-family: 'Source Sans Pro', Arial !important;
    /*** YOUR CHOSEN FONT FAMILY ***/
    font-size: 20px;
    color: #fff;
    /*** COLOR OF YOUR TEXT ***/
    text-align: center;
    width: 100%;
    padding: 5px 0;
}

但它不适用于这个简码(它适用于 id)......我的页面显示非常糟糕(只有菜单!不是内容,好像它在加载所有内容之前就崩溃了),没有错误消息在控制台和响应 200 中,没有错误...

你能帮帮我吗? 经过 7 小时的搜索,我擦干了……

非常感谢

伊娃

PS : 我用 Divi

【问题讨论】:

    标签: php wordpress shortcode divi


    【解决方案1】:

    WordPress/Woocommerce 具有当前帖子/产品的全局变量。

    以下是两个建议,尝试使用您的简码并检查哪个适合您:

    1. 使用全局后置变量: 全球 $post; $id = $post->ID;
    2. 使用全局产品变量: 全球$产品; $id = $product->get_id();

    希望它有效!祝你好运。

    【讨论】:

    • 您好!非常感谢您的回答。我尝试了这两个,但我没有第一个的生产信息(只有实际页面的信息),第二个是“假”所以我不知道必须有信息......我认为第二个不起作用,因为它是在 CSS 中调用的?谢谢!!
    猜你喜欢
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多