【发布时间】:2017-04-26 02:47:38
【问题描述】:
我想知道是否有办法将我的 Wordpress 附件链接放入 javascript 函数中。简单来说,它的功能如下:
$attach_url = "<?php echo wp_get_attachment_url(); ?>";
function () {
return $attach_url;
}
更具体地说,我正在寻找一种使用 Photoswipe 的分享按钮(完整源代码here)来实现它的方法:
$attach_url = = "<?php echo wp_get_attachment_url(); ?>";
(this, function () {
'use strict';
var PhotoSwipeUI_Default =
function(pswp, framework) {
shareButtons: [
{url:'https://www.facebook.com/sharer/sharer.php?u={{attachment_pg_url}}'},
],
getAttachmentPageURL: function ( shareButtonData ) {
return $attach_url;},
parseShareButtonOut: function(shareButtonData, shareButtonOut) {
return shareButtonOut;
},
_updateShareURLs = function() {
var shareButtonOut = '',
shareButtonData,
shareURL,
attachment_pg_url;
for(var i = 0; i < _options.shareButtons.length; i++) {
shareButtonData = _options.shareButtons[i];
attachment_pg_url = _options.getAttachmentPageURL(shareButtonData);
shareURL = shareButtonData.url.replace('{{attachment_pg_url}}', attachment_pg_url );
非常感谢任何帮助!
编辑
这是我更新的代码,几乎可以使用。 js 文件正在解释来自functions.php 的入队脚本。但是,显示的 url 是<?php echo get_attachment_link(); ?>,而不是实际链接(例如:home.com/attachment-page),当我在循环中使用此 php 代码时,它确实显示正确。
如何获取 url 来输出链接,而不是实际的 php 代码?
在functions.php中:
// Custom Photoswipe Share URL
wp_enqueue_script( 'photoswipe_custom_share_link', get_template_directory_uri() . '/custom_path_here/photoswipe-ui-single-item.js' );
$attach_url = "<?php echo get_attachment_link(); ?>";
wp_localize_script( 'photoswipe_custom_share_link', 'pswp_custom_share', $attach_url );
在 Photoswipe js 文件中(在我的原始帖子中进行了一些额外的修复):
(this, function () {
'use strict';
var PhotoSwipeUI_Default =
function(pswp, framework) {
shareButtons: [
{url:'https://www.facebook.com/sharer/sharer.php?u={{attach_url}}'},
],
getAttachmentURLForShare: function ( /*shareButtonData */) {
return pswp_custom_share;
},
parseShareButtonOut: function(shareButtonData, shareButtonOut) {
return shareButtonOut;
},
_updateShareURLs = function() {
var shareButtonOut = '',
shareButtonData,
shareURL,
attachment_url;
for(var i = 0; i < _options.shareButtons.length; i++) {
shareButtonData = _options.shareButtons[i];
attachment_url = _options.getAttachmentURLForShare(shareButtonData);
shareURL = shareButtonData.url.replace('{{attach_url}}', encodeURIComponent(attachment_url) );
编辑
我在入队脚本中使用引号时犯了一个错误。使用下面的代码,格式现在是正确的。唯一的问题是 url 输出是“home.com”而不是“home.com/attachment-page”。
如何在循环之外定义动态生成的附件页面 url?我需要回应吗?
$attach_url = get_attachment_link($attachment->ID);
编辑 - 已解决!
我需要使用 JavaScript enqueue 在循环中获取附件的基本 url。 (基于答案here 并在 thedarkcoder 的帮助下)。
在functions.php中:
// Custom Photoswipe Share URL
function pswp_custom_share()
{
/* Get the ID of the current post */
global $post;
$ID = $post->ID;
/* register the script */
wp_register_script( 'photoswipe_custom_share_link', get_template_directory_uri() . 'custom_path_here/photoswipe-ui-single-item.js', array('jquery'), false, true );
$attach_url = array(
'attachment_page' => get_attachment_link( $ID )
);
wp_enqueue_script( 'photoswipe_custom_share_link' );
wp_localize_script('photoswipe_custom_share_link', 'attach_url', $attach_url);
}
/* If we're not in the admin section call our function on the wp_enqueue_scripts hook */
if ( !is_admin() ) add_action( "wp_enqueue_scripts", "pswp_custom_share", 10 );
在Photoswipe js文件中:
getAttachmentURLForShare: function ( /*shareButtonData */) {
return attach_url.attachment_page;
},
【问题讨论】:
标签: javascript php wordpress attachment photoswipe