开发者

Mixing PHP and HTML in variable

开发者 https://www.devze.com 2023-02-20 14:00 出处:网络
I\'m trying to create a Wordpress plugin that adds a button below all posts. The button is a \"Like\" button that pass some parameters through the URL to a new site.

I'm trying to create a Wordpress plugin that adds a button below all posts. The button is a "Like" button that pass some parameters through the URL to a new site.

The parameters are Wordpress permalink, title, and blog name.

Can't make it work.

function add_bloglovin($content) {

   $blog_title = get_bl开发者_如何学Googinfo('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);


the_permalink() is a display function. Use get_permalink() to return a string that you can use. To make the_title return just the title with no wrapped HTML you need to use 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;
}


From the Wordpress Codex: http://codex.wordpress.org/Function_Reference/the_permalink

Function Reference/the permalink

Displays the URL for the permalink to the post currently being processed in The Loop. This tag must be within The Loop, and is generally used to display the permalink for each post, when the posts are being displayed. Since this template tag is limited to displaying the permalink for the post that is being processed, you cannot use it to display the permalink to an arbitrary post on your weblog.

You can't use $link = the_permalink(); in isolation unless it's in the Loop.


Try a basic sanity check by var_dump()ing the $link and $title variables. Do they actually contain a string?

0

精彩评论

暂无评论...
验证码 换一张
取 消