如何在WordPress的使用the_permalink()时,它与'或\”包装(How t

2019-10-20 17:58发布

我有一个包装上与外部的div(用于共享按钮上的图像时,悬停)员额,每幅图像的编码。 问题是,当我想写the_permalink(); 在function.php,它的包裹“或”在一个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>

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

应该管用。



文章来源: How to use the_permalink() on wordpress when it's wrapped with ' or \"