javascript - 如何在 JS 文件中添加 wordpress 永久链接(onclick、window.open)?

标签 javascript php wordpress facebook

单击时我想打开一个弹出窗口,允许访问者通过 FACEBOOk 共享我的博客。一切正常,但问题是我无法获得当前文章的永久链接。如何在javascript中添加php?

// Replace the images with the following div and pin button
    $button_div = '<div class="plugin">
      <div class="imgshare">
        <div class="postfb">
            <a onclick="popItUp()">
                <img src="http://www.example.com/wp-content/facebook.png" width="45px" />
            </a>
        </div>

      </div>
      <div class="demo"><img$1src="$2.$3" $4 width="$5" height="$6" /></div>
    </div>';

    // Replace the images with a containing div with a pin button on the image
    $content = preg_replace( $pattern, $button_div, $content );

    return $content;
    }
}

<script>
    function popItUp(url) {
        var newWindow = window.open('http://www.facebook.com/share.php?u=<?php the_permalink(); ?>&title=<?php the_title(); ?>', 'name', 'height=615,width=475,scrollbars=yes');

        if (newWindow.focus) {
            newWindow.focus();
        }
    }
</script>

最佳答案

the_permalink() 仅在“循环”中有效。

我假设此代码在主帖子循环之外,在这种情况下,您需要使用 get_permalink() 并传递正在查看的帖子的 ID。

您可以通过从$wp_query中获取post对象来获取当前post的ID;

function getPostID(){
    $post = $wp_query->post;
    return $post->ID;
}

现在您可以使用:

<?php echo get_permalink(getPostID()) ?>

关于javascript - 如何在 JS 文件中添加 wordpress 永久链接(onclick、window.open)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29348085/

相关文章:

javascript - 从 0 到 x 的 CSS 宽度转换导致内容压缩

c# - 从 c# 后面的代码编写页面加载 javascript 函数

php - 在 PHP 中设置文本框的最大值

javascript - 如何在表格单元格中插入弹出窗口?

php - 适合初学者的 PHP 动态页面?

php - 安全密码存储

php - 如何合并两个没有唯一键的关联数组的输出?

css - Wordpress:导航边框底部

wordpress - (Docker) 如何在 WordPress 容器中使用单独的 Composer 容器安装依赖项?

php - 如何在 AJAX 上接收 PHP 表,并将其显示在 DIV 中?