2013-02-06 27 views
0

我已經搜索了一段時間的這個問題,也許它很簡單,也許沒有。我無法弄清楚如何讓這個工作。

我的目標結果將是一個超鏈接與後meta有關的一些樣式如此。

<a href="href_link" style="color: #e67300" rel="nofollow"> Check out the r_title here!</a> 

我的代碼是:

<?php 
$rtitle1 = get_post_meta($post->ID, 'r_title', true); 
$rlink1 = get_post_meta($post->ID, 'href_link', true); 
    function testfunction() { 

    $output .= '<a href=\"'$rlink1'\" style=\"color: #e67300\" rel=\"nofollow\">'; 
    $output .= ' Check out the '$rtitle1' here!</a>'; 

    return $output; 
    } 
add_shortcode('shortcode', 'testfunction'); 
?> 
+0

你不需要在單引號內換掉雙引號 –

回答

1

有幾個問題與您的代碼。第一個問題是string concatenation。當你想膠水串在一起,你需要使用concatenation operator(點:.):

$end = 'a string'; 
$start = 'This is '; 
$string = $start.$end; 

如果你只是並列變量和字符串(或任何其他scalar types),那麼你會得到錯誤:

​​

第二個問題是您正在使用global scope中的兩個變量($rtitle1$rlink1)。如果你想用一個函數內部全局變量,那麼你需要將其申報爲世界裏面的功能:

$globalVar = 'test'; 
function test() { 
    global $globalVar; 
    echo $globalVar; 
} 

第三個問題是,你忘了結尾右括號,),爲get_post_meta()功能:

$rtitle1 = get_post_meta($post->ID, 'r_title', true; 
$rlink1 = get_post_meta($post->ID, 'href_link', true; 

他們應該是這樣的:

$rtitle1 = get_post_meta($post->ID, 'r_title', true); 
$rlink1 = get_post_meta($post->ID, 'href_link', true); 

之前,你想尋求幫助,你應該看看電子你得到的恐怖信息。如果你之前還沒有看到過錯誤信息,那就Google吧。學習一些最好的方法是自己找到解決方案。提出問題是因爲您嘗試找到解決方案但找不到它。

+0

對不起,我會開始這樣做,但我不確定如何。我如何在我的代碼中發現錯誤,以便在我的WordPress網站上測試它們? – user2046471

+0

如果您的代碼中存在錯誤,那麼它們將出現在您的網站上。他們通常看起來像這樣:「'解析錯誤:語法錯誤,在...'中出乎意料的T_IF」。如果你沒有看到任何錯誤,那麼看看這個問題的答案:http://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings –