[英]How to use the_permalink() on wordpress when it's wrapped with ' or "
I have a code that wraps every image on posts with an external div (for share buttons when hovering on images). 我有一个代码,用外部div将每个图像包装在帖子上(将鼠标悬停在图像上时用于共享按钮)。 The thing is, when I want to write the_permalink(); 问题是,当我想编写the_permalink()时; in the function.php, it's wrapped inside a ' or a " in the a href tags. 在function.php中,它包装在a href标记中的'或'内。
That causes share links look like this: 这导致共享链接如下所示:
https://plus.google.com/share?url=%3C?php%20the_permalink();%20?%3E https://plus.google.com/share?url=%3C?php%20the_permalink();%20?%3E
This is the code in function.php: 这是function.php中的代码:
function breezer_addDivToImage( $content ) {
// A regular expression of what to look for.
$pattern = '/(<img([^>]*)>)/i';
// What to replace it with. $1 refers to the content in the first 'capture group', in parentheses above
$the_url = the_permalink();
$replacement = '<div class="imgWrap">
$1
<div class="imgDescription">
<div class="theShareLinks">
<img src="http://localhost/mySite/wp-content/uploads/2014/08/dfc2.png" />
<a href="http://twitter.com/share?text=&url=<?php the_permalink(); ?>" class="img-twitter" title="Share on Twitter"></a>
<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>?" title="Share on Facebook" onclick="window.open(this.href, \'newwin\', \'width=500, height=200\'); return false;" class="img-facebook"></a>
<a href="https://plus.google.com/share?url=<?php the_permalink(); ?>" class="img-google" title="Share on Google"></a>
</div>
</div>
</div>';
// run preg_replace() on the $content
$content = preg_replace( $pattern, $replacement, $content );
// return the processed content
return $content;
}
add_filter( 'the_content', 'breezer_addDivToImage' );
These are the lines from the code above^ that are wrapped in the " " : 这些是上面代码^中的行,这些行都包装在“”中:
<a href="http://twitter.com/share?text=&url=<?php the_permalink(); ?>" class="img-twitter" title="Share on Twitter"></a>
<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>?" title="Share on Facebook" onclick="window.open(this.href, \'newwin\', \'width=500, height=200\'); return false;" class="img-facebook"></a>
<a href="https://plus.google.com/share?url=<?php the_permalink(); ?>" class="img-google" title="Share on Google"></a>
Changing: 更改:
<a href="http://twitter.com/share?text=&url=<?php the_permalink(); ?>" class="img-twitter" title="Share on Twitter"></a>
to 至
<a href="http://twitter.com/share?text=&url='.get_the_permalink() .'" class="img-twitter" title="Share on Twitter"></a>
should work. 应该管用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.