当用'或'包装时,如何在wordpress上使用the_permalink()

时间:2014-08-21 08:58:53

标签: php wordpress-plugin wordpress

我有一个代码用外部div包装帖子上的每个图像(当悬停在图像上时用于共享按钮)。 问题是,当我想写the_permalink();在function.php中,它被包装在a href标签中的'或'中。

这导致共享链接看起来像这样:

https://plus.google.com/share?url=%3C?php%20the_permalink();%20?%3E

这是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' );

以上代码中的行包含在“”:

<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>

1 个答案:

答案 0 :(得分:1)

更改:

 <a href="http://twitter.com/share?text=&url=<?php the_permalink(); ?>" class="img-twitter" title="Share on Twitter"></a>

<a href="http://twitter.com/share?text=&url='.get_the_permalink() .'" class="img-twitter" title="Share on Twitter"></a>

应该有效。