Wordpress短代码中的链接在最后增加了额外的空间

时间:2014-03-27 05:54:57

标签: php html wordpress shortcode

我为wordpress网站上的工具提示创建了一个短代码。短代码只是一个链接,当你将鼠标悬停在链接上时会显示工具提示,虽然由于某种原因使用短代码时它会在链接末尾添加额外的空间,我无法弄清楚如何摆脱它。您可以在我们网站的页面上看到问题

http://stormable.com/hero-lore-tyrande-whisperwind/如果您查看内容中的任何链接,您会在链接后看到额外的空格。

add_shortcode('tyrande', 'tyrande');
function tyrande( $atts, $content = null ) {

// Hero Meta Data
$HeroID = 337;
$Hero = Tyrande;
$hero = tyrande;
$franchise = warcraft;


$heroSubname = get_post_meta( $HeroID, 'hero-sub-name', true);

$health_gain = get_post_meta( $HeroID, 'health-gain', true); 
$heroHealth = $health_gain*24+get_post_meta($HeroID, "health-lvl1", true);

$healthRegenGain = get_post_meta( $HeroID, 'health-regen-gain', true);
$heroHealthRegen = $healthRegenGain*24+get_post_meta($HeroID, "health-regen-lvl1", true); 

$manaGain = get_post_meta( $HeroID, 'mana-gain', true);
$heroMana = $manaGain*24+get_post_meta($HeroID, "mana-lvl1", true);

$manaRegenGain = get_post_meta( $HeroID, 'mana-regen-gain', true);
$heroManaRegen = $manaRegenGain*24+get_post_meta($HeroID, "mana-regen-lvl1", true); 

$attackSpeedGain = get_post_meta( $HeroID, 'attack-speed-gain', true);
$heroAttackSpeed = $attackSpeedGain*24+get_post_meta($HeroID, "attack-speed-lvl1", true);

$attackDamageGain = get_post_meta( $HeroID, 'attack-damage-gain', true);
$heroAttackDamage = $attackDamageGain*24+get_post_meta($HeroID, "attack-damage-lvl1", true);

extract( shortcode_atts( array( 'icon' => 'false'), $atts ) );

    if($icon == 'true'){
        $output = '<img class="hero-tt-icon" src="http://stormable.com/img/heroes/' . $hero . '/' . $hero . '-tt-icon.png" alt="' . $hero . ' Heroes of the Storm">';
    }

$output .= '
    <a class="tooltip" 
    href="http://stormable.com/heroes/' . $hero . '/">' . $Hero . '
        <span class="hero-tooltip">
            <span class="hero-tt-image"><img src="http://stormable.com/img/heroes/' . $hero . '/' . $hero . '-tt.png" alt="Heroes of the Storm ' . $Hero . '" /></span>
            <span class="hero-tt-info">
                <span class="hero-tt-name-info">
                    <span class="hero-tt-fran"><img src="http://stormable.com/img/icons/' . $franchise . '.png" alt="Heroes of the Storm ' . $franchise . ' Franchise" /></span>
                    <span class="hero-tt-name">' . $Hero . '</span>
                    <span class="hero-tt-subname">' . $heroSubname . '</span>
                </span>
                <span class="HeroStats">
                    <span class="HeroStats-title">Hero Stats at Level 25</span>
                    <span class="HeroStats-row">
                        <span class="HeroStat-left">Health</span>
                        <span class="HeroStat-right">' . $heroHealth . ' (' . $health_gain . ' / level)</span>
                    </span>
                    <span class="HeroStats-row">
                        <span class="HeroStat-left">Health Regen</span>
                        <span class="HeroStat-right">' . $heroHealthRegen . ' (' . $healthRegenGain . ' / level)</span>
                    </span>
                    <span class="HeroStats-row">
                        <span class="HeroStat-left">Mana</span>
                        <span class="HeroStat-right">' . $heroMana . ' (' . $manaGain . ' / level)</span>
                    </span>
                    <span class="HeroStats-row">
                        <span class="HeroStat-left">Mana Regen</span>
                        <span class="HeroStat-right">' . $heroManaRegen . ' (' . $manaRegenGain . ' / level)</span>
                    </span>
                    <span class="HeroStats-row">
                        <span class="HeroStat-left">Attack Speed</span>
                        <span class="HeroStat-right">' . $heroAttackSpeed . ' (' . $attackSpeedGain . ' / level)</span>
                    </span>
                    <span class="HeroStats-row">
                        <span class="HeroStat-left">Attack Damage</span>
                        <span class="HeroStat-right">' . $heroAttackDamage . ' (' . $attackDamageGain . ' / level)</span>
                    </span>
                </span>
            <span class="clear"></span>
        </span>
    </a>
';

return $output;

}

1 个答案:

答案 0 :(得分:0)

我建议您使用 HEREDOC 在PHP中生成HTML。

对于快速解决方案,请使用preg_replace删除不需要的空间&amp;输出中的新行。在return $output声明之前添加以下行。

$output =  preg_replace('/^\s+|\n|\r|\s+$/m', '', $output);