2015-06-20 75 views
0

我花了大約2個小時試圖混合2個代碼工作,並感受到我的代碼技巧的弱點,我在此尋求幫助。如何切換Tweet Me按鈕的(單/雙)引號

我覺得問題在於單/雙引號。我嘗試了很多次,重新格式化引號(以及兩種方式,即將兩個腳本重新格式化以適應彼此)。

的圖像(其應該鏈接)加入到在functions.php的頁面 - 的圖像是路徑/ image.png在這個例子中:

! is_admin() && add_filter('the_content', function($content) 
{ 
    if(in_the_loop()) // <-- Target the main loop 
    { 
     $prepend = "<div style='color:#808080 ; border:1px solid #909090 ; border-radius:5px; float:left; padding-top:1px;'>&nbsp;<**img src='/path/image.png' alt='Tweet this' style='margin-bottom: -4px; '**>Tweet this &#8202;</div>&nbsp;"; 
     $content = $prepend . $content; 
    } 
    return $content; 
}, 9); 

這是鏈接 - 當用戶點擊圖像,我希望他們能夠訪問此鏈接:

<a href='https://twitter.com/share?url=<?php echo $post_url ?>&text=<?php echo $post_title ?>' onclick='window.open(this.href,"popupwindow", "width=800,height=500,left=200,top=5,scrollbars,toolbar=0,resizable"); return false;' Target='_blank' >twitter</a> 

我想直到結束是:點擊圖片頁面上,(它會打開Twitter的鏈接)用戶到達鳴叫(分享)他們在Twitter上發佈的帖子。

因此,用戶點擊圖像(路徑/ image.png)並打開鏈接(twitter.com ...等)。

+0

你的意思是'href'你的第二代碼部分的值應該在第一部分的'$ prepend'值中進行,直到(當前)'/ path/image.png'?但是第二部分中的<?php echo ...?>呢?在合併鏈接之前它們被替換了嗎?請給出一個清晰的完整的計劃結構。 – cFreed

+0

嗨,謝謝你的迴應。我只想把path/image.png並將該圖像鏈接到第二段代碼中的鏈接。我想結束第一段代碼,但是將第二段代碼中的鏈接添加到該圖片。我不確定我是否充分描述了這一點。 –

+0

對不起,但我仍然不確定。請用明確的「這是我想要結束的」例子來編輯你的問題,在這裏你突出顯示來自其他地方的部分。像'期望的結果字符串 - **這個來自...... ** - 結果字符串的結尾'。 – cFreed

回答

0

仍然不確定任何事情:你應該認識到你的問題缺乏清晰度。
值得注意的是,因爲你沒有說$post_url$post_title來自哪裏,也沒有解釋什麼是$prepend$content,也沒有解釋你的函數的所有用法和上下文。

但我會談談我相信理解的內容。

你的目標似乎是導致這種HTML部分:

<div style="color:#808080; border:1px solid #909090; border-radius:5px; float:left; padding-top:1px;"> 
    <a href="https://twitter.com/share?url=the-post_url&text=the-post_title" 
    onclick="window.open(this.href,'popupwindow','width=800,height=500,left=200,top=5,scrollbars,toolbar=0,resizable'); return false;" 
    target="_blank"> 
     <img src="/path/image.png" alt="Tweet this" style="margin-bottom:-4px;"> 
     Tweet this &#8202; 
    </a> 
</div> 

(你會發現,我已經返回到任意給優先級爲雙引號,這是最標準的選擇)

說我們叫$div$a,並$img源主要部件,輸出所需塊,我們只需寫類似:
echo $div . $a . $img . 'Tweet this &#8202;</a></div';

我想你報告的問題來自你必須定義這些組件作爲字符串,而他們已經包括簡單和雙引號。

所以答案是使用PHP heredoc syntax

定界符文本表現的就像一個雙引號字符串,而雙引號。這意味着heredoc中的引號不需要轉義,但上面列出的轉義碼仍然可以使用。變量被擴展了,但是當像在字符串中那樣在heredoc中表達複雜的變量時必須同樣小心。

以及組件的定義應該是這樣的:

$div = <<<EOT 
<div style="color:#808080; border:1px solid #909090; border-radius:5px; float:left; padding-top:1px;"> 
EOT; 
$a = <<<EOT 
<a href="https://twitter.com/share?url=$post_url&text=$post_title" onclick="window.open(this.href,'popupwindow','width=800,height=500,left=200,top=5,scrollbars,toolbar=0,resizable'); return false;" target="_blank"> 
EOT; 
$img = <<<EOT 
<img src="/path/image.png" alt="Tweet this" style="margin-bottom:-4px;"> 
EOT; 

事實上,許多其他變化可被視爲,特別是直接使用了整個零件的唯一變量...


編輯:

這裏是一個工作整體片斷,整合後爲$post_url$post_title值和糾正在echo...一個錯字:

$post_url="the-post-url"; 
$post_title="the-post-title"; 
$div = <<<EOT 
<div style="color:#808080; border:1px solid #909090; border-radius:5px; float:left; padding-top:1px;"> 
EOT; 
$a = <<<EOT 
<a href="https://twitter.com/share?url=$post_url&text=$post_title" onclick="window.open(this.href,'popupwindow','width=800,height=500,left=200,top=5,scrollbars,toolbar=0,resizable'); return false;" target="_blank"> 
EOT; 
$img = <<<EOT 
<img src="/path/image.png" alt="Tweet this" style="margin-bottom:-4px;" /> 
EOT; 
echo $div . $a . $img . 'Tweet this &#8202;</a></div>'; 
+0

對不起。我現在看到我不清楚的地方。在這種情況下,我並不認爲使用這些功能很重要。 $ PostURL和$ PostTitle是來自Twitter的腳本的一部分 - 整行只是複製和粘貼從https://dev.twitter.com/web/tweet-button鳴叫按鈕。)我只是想包裹圖像(或div)與簡單的,它在最基本的HTML中完成的方式。但我不能,因爲我不能讓單引號和雙引號一起工作。我嘗試了兩種解決方案,但不幸的是無法讓這些解決方案起作用。謝謝你嘗試。 –

+0

謝謝,先生。最後一段代碼工作得很好。人們,請把cFreed放在這裏。他在這裏幫助一些新手編碼的承諾真的是一些東西! –