如何切换“发推”按钮的 (single/double) 引号

How to switch the (single/double) quotes for a Tweet me button

我花了大约 2 个小时尝试混合 2 个代码,感觉我的代码技能薄弱,我在这里寻求帮助。

我认为问题出在 single/double 引号上。我尝试了很多次来重新格式化引文(以及两种方式,即重新格式化两个脚本以相互适应)。

图像(应该 linked)被添加到 functions.php 中的页面 -- 在这个例子中图像是 path/image.png:

! is_admin() && add_filter( 'the_content', function( $content )
{
    if( in_the_loop() )   // <-- Target the main loop
    {
        $prepend = "<div style='color:#808080  ; border:1px solid #909090 ; border-radius:5px; float:left; padding-top:1px;'>&nbsp;<**img src='/path/image.png' alt='Tweet this' style='margin-bottom: -4px; '**>Tweet this &#8202;</div>&nbsp;";
        $content = $prepend . $content;
    } 
    return $content;
}, 9 ); 

这是 link -- 当用户点击图片时,我希望他们访问这个 link:

<a href='https://twitter.com/share?url=<?php echo $post_url ?>&text=<?php echo $post_title ?>' onclick='window.open(this.href,"popupwindow", "width=800,height=500,left=200,top=5,scrollbars,toolbar=0,resizable"); return false;' Target='_blank' >twitter</a>

我想要结束的是:单击页面上的图像,(它打开 Twitter link,)用户可以发推文(分享)他们在 post推特

所以用户点击图片 (path/image.png) 并打开 link (twitter.com...等).

仍然不确定:您应该意识到您的问题不够清晰。
值得注意的是,因为您没有说明 $post_url$post_title 来自哪里,也没有解释什么是 $prepend$content,并且根本没有解释函数的用途和上下文.

但我会谈谈我认为已经理解的。

您的目标似乎导致了这种 HTML 部分:

<div style="color:#808080; border:1px solid #909090; border-radius:5px; float:left; padding-top:1px;">
    <a href="https://twitter.com/share?url=the-post_url&text=the-post_title"
    onclick="window.open(this.href,'popupwindow','width=800,height=500,left=200,top=5,scrollbars,toolbar=0,resizable'); return false;"
    target="_blank">
        <img src="/path/image.png" alt="Tweet this" style="margin-bottom:-4px;">
        Tweet this &#8202;
    </a>
</div>

(你会注意到我又回到了任意优先双引号,这是最标准的选择)

假设我们调用 $div$a$img 源主要组件,要输出所需的块,我们只需编写如下内容:
echo $div . $a . $img . 'Tweet this &#8202;</a></div';

我想您报告的问题是因为您必须将这些组件定义为字符串,而它们已经包含单引号和双引号。

所以答案是使用 PHP heredoc syntax:

Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.

组件定义应如下所示:

$div = <<<EOT
<div style="color:#808080; border:1px solid #909090; border-radius:5px; float:left; padding-top:1px;">
EOT;
$a = <<<EOT
<a href="https://twitter.com/share?url=$post_url&text=$post_title"  onclick="window.open(this.href,'popupwindow','width=800,height=500,left=200,top=5,scrollbars,toolbar=0,resizable'); return false;" target="_blank">
EOT;
$img = <<<EOT
<img src="/path/image.png" alt="Tweet this" style="margin-bottom:-4px;">
EOT;

事实上,可以考虑许多其他变体,特别是直接对整个部分使用唯一变量...


编辑:

这是一个有效的完整代码段,在整合了 $post_url$post_title 的值并更正了 echo... 中的拼写错误之后:

$post_url="the-post-url";
$post_title="the-post-title";
$div = <<<EOT
<div style="color:#808080; border:1px solid #909090; border-radius:5px; float:left; padding-top:1px;">
EOT;
$a = <<<EOT
<a href="https://twitter.com/share?url=$post_url&text=$post_title"  onclick="window.open(this.href,'popupwindow','width=800,height=500,left=200,top=5,scrollbars,toolbar=0,resizable'); return false;" target="_blank">
EOT;
$img = <<<EOT
<img src="/path/image.png" alt="Tweet this" style="margin-bottom:-4px;" />
EOT;
echo $div . $a . $img . 'Tweet this &#8202;</a></div>';