子比文章新增功能 – Hover 文字显示 Tooltip

2026-01-16 0 436

演示截图

子比文章新增功能 – Hover 文字显示 Tooltip

子比文章新增功能 – Hover 文字显示 Tooltip

获取代码

 

子比文章新增功能 – Hover 文字显示 Tooltip子比文章新增功能 – Hover 文字显示 Tooltip

[hidecontent type=”reply” desc=”隐藏内容:评论后查看”]

/**
 * Hover文字显示Tooltip(支持文字/图片)
 * 文字提示:[tooltip text="显示的文字" tip="提示内容" placement="top"]
 * 图片提示:[tooltip text="显示的文字" img="图片URL" placement="top"]
 * placement可选值:top, bottom, left, right
 */
function zib_hover_tooltip_shortcode($atts) {
    $atts = shortcode_atts(array(
        'text' => '',
        'tip' => '',
        'img' => '',
        'placement' => 'top',
        'width' => '200'
    ), $atts);
    
    if (!$atts['text']) return '';
    
    $placement = in_array($atts['placement'], array('top', 'bottom', 'left', 'right')) ? $atts['placement'] : 'top';
    
    // 图片提示
    if ($atts['img']) {
        $content = '<img src="' . esc_url($atts['img']) . '" style="max-width:' . intval($atts['width']) . 'px;height:auto;">';
        return '<span class="zib-tip" data-toggle="popover" data-trigger="hover" data-placement="' . $placement . '" data-html="true" data-content="' . esc_attr($content) . '">' . esc_html($atts['text']) . '</span>';
    }
    
    // 文字提示
    if ($atts['tip']) {
        return '<span class="zib-tip" data-toggle="tooltip" data-placement="' . $placement . '" title="' . esc_attr($atts['tip']) . '">' . esc_html($atts['text']) . '</span>';
    }
    
    return $atts['text'];
}
add_shortcode('tooltip', 'zib_hover_tooltip_shortcode');

function zib_tooltip_init() {
    echo '<style>.zib-tip{border-bottom:1px dashed;cursor:help}</style>';
    echo '<script>jQuery(function($){$("[data-toggle=tooltip]").tooltip();$("[data-toggle=popover]").popover({trigger:"hover",html:true,container:"body"})});</script>';
}
add_action('wp_head', 'zib_tooltip_init');
add_action('wp_footer', 'zib_tooltip_init');

 

[/hidecontent]

 

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

轩玮博客 子比主题 子比文章新增功能 – Hover 文字显示 Tooltip http://blog.xuwbk.com/1115.html

常见问题

相关文章

猜你喜欢
发表评论
1 条评论
2026年2月2日 下午1:38 回复

楼主听话,快到碗里来!

官方客服团队

为您解决烦忧 - 24小时在线 专业服务