Javascript内部的Wordpress PHP链接(用于Photosweep自定义共享URL)
我想知道是否有办法将我的Wordpress附件链接放入javascript函数中。简单地说,它的功能如下:Javascript内部的Wordpress PHP链接(用于Photosweep自定义共享URL),javascript,php,wordpress,attachment,photoswipe,Javascript,Php,Wordpress,Attachment,Photoswipe,我想知道是否有办法将我的Wordpress附件链接放入javascript函数中。简单地说,它的功能如下: $attach_url = "<?php echo wp_get_attachment_url(); ?>"; function () { return $attach_url; } 编辑-已解决 我需要使用JavaScript排队来获取循环中附件的基本url。(基于答案并在暗码员的帮助下) 在functions.php中: // Custom Photosw
$attach_url = "<?php echo wp_get_attachment_url(); ?>";
function () {
return $attach_url;
}
编辑-已解决强>
我需要使用JavaScript排队来获取循环中附件的基本url。(基于答案并在暗码员的帮助下)
在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 );
// 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 );
在Photosweep js文件中:
getAttachmentURLForShare: function ( /*shareButtonData */) {
return attach_url.attachment_page;
},
您可以使用WordPress排队脚本函数来实现这一点 详细信息请点击下面的链接
让我知道它是否解决了您的问题这在理论上应该是可能的,因为在客户端运行javascript之前,PHP是在服务器端处理的 我注意到您的脚本缺少wp_get_attachment_url函数中的附件ID 来自Wordpress API文档:
<?php echo wp_get_attachment_url( 12 ); ?>
第一步是通过将附件URL回显到页面,在基本级别上测试功能。完成此操作后,您就知道您正确地连接到了函数
然后将该值分配给JavaScript变量,并尝试运行某种警报或控制台日志,以验证是否可以成功地将URL解析为全局JS变量
然后看看是否可以从函数内部访问该变量。如果你能一步一步地完成上面的每一个基本步骤,那么你就完全有可能将这些数据解析到Photosweep插件中
如果您遇到困难,请告诉我,我会尽力帮助您。因此我应该在functions.php中使用以下代码:
wp\u enqueue\u脚本('photoswip\u custom\u share\u link',get\u template\u directory\u uri()。/custom\u path\u here/photoswip ui single item.js')$附上_url=“”;wp_本地化_脚本('photoswip_custom_share_link'、'pswp_custom_share'、$attach_url)代码>然后在我的javascript中引用为:getAttachmentPageURL:function(shareButtonData){return pswp_custom_share;},
?谢谢,它正在工作,尽管它输出的是php代码本身,而不是链接。上面是我的完整编辑。根据编辑,唯一的问题是显示的url是,
,而不是php应该输出的实际链接。有什么想法吗?再编辑一次(以上)!非常接近。问题是它输出的是“home.com”,而不是“home.com/attachment page”。get_attachment_link()
应该生成完整的url,包括斜杠。我认为它无法从循环中获取附件ID信息,因此不知道使用哪个页面。P.S.Stack overflow要求我“将此讨论移动到聊天室”。谢谢,您帮助我意识到我实际上需要使用
。我已经在上面加入了我的编辑。
// 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 );
getAttachmentURLForShare: function ( /*shareButtonData */) {
return attach_url.attachment_page;
},
<?php echo wp_get_attachment_url( 12 ); ?>