2014-01-06 20 views
2

當我使用這個簡碼時,cat_titles不在div的內部? check out the examplePHP:shortcode不保留在div標記

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'); 
+0

它應該是'return',你已經使用了'echo'。簡碼應該返回值,而不是'echo'。請檢查[參考](http://codex.wordpress.org/Function_Reference/add_shortcode)。 – Shazzad

回答

3

嘗試改變single_cat_title('', true)single_cat_title('', false)

我認爲這是使用它在PHP中,而不是顯示

+0

錯誤是正確的。 http://codex.wordpress.org/Function_Reference/single_cat_title,但你的推理是不正確的,這取決於你對'那個'的定義。 :P – Sanchit

+0

$ display (布爾值)(可選)頁面標題是否應顯示(true)或返回以用於PHP(false)。 –

0

您應該使用的return代替echo

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

當您使用echo,當正在處理您的函數短碼立即打印。

另外,如Gert suggested,將$display參數添加到false

0

我寧願使用這樣的insted。

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'); 
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');