如何解决在悬停时显示的自定义短代码
我想在我的产品上(在类别页面中)以悬停方式显示自定义属性,但我遇到了困难。在我看来,最简单的方法是创建一个短代码,但我无法克服它...... 这就是我所在的地方。 这个,使用 [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
解决方法
WordPress/Woocommerce 具有当前帖子/产品的全局变量。
以下是两个建议,尝试输入您的短代码并检查哪个适合您:
- 使用全局 post 变量: 全球 $post; $id = $post->ID;
- 使用全局产品变量: 全球$产品; $id = $product->get_id();
希望它有效!祝你好运。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。