2011-03-30 50 views
1

我試圖創建一個Wordpress插件,它在所有帖子下面添加一個按鈕。該按鈕是一個「贊」按鈕,通過URL將一些參數傳遞到新站點。在變量中混合使用PHP和HTML

參數是WordPress的永久鏈接,標題和博客名稱。

無法使其工作。

function add_bloglovin($content) { 

    $blog_title = get_bloginfo('name'); 
    $link = the_permalink(); 
    $title = the_title(); 

    $bloglovin ="<br><a href=\"http://www.bloglovin.com/like/?b=$blog_title&p=$link&t=$title\" onclick=\"window.open(this.href, 'bloglovin_like', 'width=480,height=320, toolbar=0, location=0, menubar=0, scrollbars=0, status=0'); return false;\"><img src=\"http://www.bloglovin.com/widget/bilder/like.gif\"></a>"; 
    return $content .$bloglovin; 
} 
add_filter('the_content', add_bloglovin); 
+0

究竟什麼不行?什麼是失敗? – JohnP 2011-03-30 10:15:52

+0

它將第一個$ blog_title添加到$ bloglovin變量中,但不是$ link和$ title ..是PHP還是wordpress,可以解決這個問題? – user681061 2011-03-30 10:17:54

+0

您可能需要在放入代碼之前將代碼urlencode()。 – JohnP 2011-03-30 10:33:04

回答

3

the_permalink()是顯示函數。使用get_permalink()返回可以使用的字符串。要讓the_title只返回沒有包裝HTML的標題,你需要使用the_title('','',false);

function add_bloglovin($content) { 
    $blog_title = get_bloginfo('name'); 
    $link = get_permalink(); 
    $title = the_title('','',false); 
    $bloglovin ="<br><a href=\"http://www.bloglovin.com/like/?b=$blog_title&p=$link&t=$title\" onclick=\"window.open(this.href, 'bloglovin_like', 'width=480,height=320, toolbar=0, location=0, menubar=0, scrollbars=0, status=0'); return false;\"><img src=\"http://www.bloglovin.com/widget/bilder/like.gif\"></a>"; 
    return $content .$bloglovin; 
} 
+0

你救了我一天的男人!十分感謝!它像一個魅力! – user681061 2011-03-30 12:40:57

1

從WordPress食品: http://codex.wordpress.org/Function_Reference/the_permalink

函數參考/固定鏈接

顯示的URL永久鏈接 目前正在 的循環後處理的。此標記必須在 循環內,並且通常用於在顯示 帖子時顯示 每篇文章的固定鏈接。由於此 模板標記僅限於顯示 正在處理的帖子的永久鏈接,因此您無法使用它來登錄 在您的博客上顯示任意 帖子的永久鏈接。

您不能使用$ link = the_permalink();除非它在the Loop之內。

+0

雖然..真正的shanethehat代碼不正確!我只是在我從wordpress獲得的變量前面使用get_,所以它在循環內部。$ content是循環內部的代碼,我在代碼後面添加了代碼。 – user681061 2011-03-30 12:42:30

+0

嘿!我說Codex說不能使用the_permalink()函數[或者get_permalink()或者事實上],除非它在循環中。你原來的帖子不會顯示它是否在循環中引用,所以我告訴你檢查一下,以防萬一。真高興你做到了。 – Cogicero 2011-03-31 14:41:18

0

嘗試通過var_dump()變量$link$title變量進行基本的健全性檢查。他們真的包含一個字符串嗎?