PHP:短代码不在div标签内

时间:2014-01-06 04:53:55

标签: php html wordpress

为什么我使用此短代码cat_titles不在div内? check out the example

function get_quote($atts) {
    echo    '<div id="le_quote">AquaPumps.Org is your online supplier of '.single_cat_title( '', true ).' Accessories, '.single_cat_title( '', true ).' Hoses and '.single_cat_title( '', true ).' Pumps.  Use our selection to compare '.single_cat_title( '', true ).' models , specs and more. Buy '.single_cat_title( '', true ).' products at the best price possible today.</div>';
}
add_shortcode('random_quote', 'get_quote');

4 个答案:

答案 0 :(得分:3)

尝试将single_cat_title('', true)更改为single_cat_title('', false)

我认为这是在php中使用它而不是显示

答案 1 :(得分:0)

您应该使用return代替echo

function get_quote($atts) {
    return 'test string';
}
add_shortcode('random_quote', 'get_quote');

当您使用echo时,会在处理您的功能时立即打印短代码。

另外,在Gert suggested时,将$display参数添加到false

答案 2 :(得分:0)

我宁愿使用这样的东西。

function get_quote($atts) {
    $title = single_cat_title( '', false);
    echo "<div id='le_quote'>AquaPumps.Org is your online supplier of $title Accessories, $title Hoses and $title Pumps. Use our selection to compare $title models, specs and more. Buy $title products at the best price possible today.</div>";
}

add_shortcode('random_quote', 'get_quote');

答案 3 :(得分:0)

将猫标题保存到变量然后回显。

function get_quote($atts) {
$cat_title = single_cat_title( '', false );
echo    '<div id="le_quote">AquaPumps.Org is your online supplier of '.$cat_title.'     
Accessories, '.$cat_title.' Hoses and '.$single_cat_title.' Pumps.  Use our selection 
to compare '.cat_title( '', true ).' models , specs and more. Buy '.cat_title.' 
products at the best price possible today.</div>';
}
add_shortcode('random_quote', 'get_quote');