2014-03-12 42 views
0

我使用的是這就是所謂的javascript「Backstretch」顯示在我的網站的時候視口越來越大或小的調整大小這回的圖像。現在我想將它與WordPress的get_post_thumbnail功能相結合,這樣我就可以將背景圖像設置爲特色圖像。WP get_post_thumbnail與JS直道

我嘗試了標準WP功能,但是,這並不工作,因爲它增加了標籤:

$.backstretch("<?php echo get_the_post_thumbnail($post_id, $size, $attr); ?>"); 

所以我需要剝離這些標籤..我越來越近,因爲我現在得到一個URL(和圖像),但它始終即使我設置的每一頁

<?php $url = wp_get_attachment_url(get_post_thumbnail_id($post_id, $size, $attr)); ?> 

<script>$.backstretch("<?php echo $url; ?>");</script> 
+0

當循環使用the_ID()代替$ POST_ID –

+0

@MichalS對不起,我不知道你是想說... –

+0

內部時,內部的https:/ /codex.wordpress.org/The_Loop(參見sam普萊斯有)使用wp_get_attachment_url(get_post_thumbnail_id(the_ID(),$大小,$ attr)使用) –

回答

0

你得到的答案,在本教程中你的問題:http://sridharkatakam.com/set-featured-image-full-sized-background-posts-pages-wordpress/

創建一個直道,set.js文件,包括 jQuery(document).ready(function($) { $("body").backstretch([BackStretchImg.src],{duration:3000,fade:750}); });

,然後排隊兩個JS-文件(backstretch.js和您的直道-set.js)到functions.php

//* Enqueue Backstretch script 
add_action('wp_enqueue_scripts', 'enqueue_backstretch'); 
function enqueue_backstretch() { 

//* Load scripts only on single Posts, static Pages and other single pages and only if featured image is present 
if (is_singular() && has_post_thumbnail()) 

    wp_enqueue_script('backstretch', get_bloginfo('stylesheet_directory') . '/js/jquery.backstretch.min.js', array('jquery'), '1.0.0'); 
    wp_enqueue_script('backstretch-set', get_bloginfo('stylesheet_directory').'/js/backstretch-set.js' , array('jquery', 'backstretch'), '1.0.0'); 

    wp_localize_script('backstretch-set', 'BackStretchImg', array('src' => wp_get_attachment_url(get_post_thumbnail_id()))); 
+2

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 –

+0

好的,我包含了教程中的必要代碼。 – kbrunner

0

嘗試使用像全局變量$ post對象等不同功能的圖像的同一個:

<?php global $post; $url = wp_get_attachment_url(get_post_thumbnail_id($post->ID, 'full')); ?> 

<script>$.backstretch("<?php echo $url; ?>");</script> 
+0

感謝您的答案,但沒有任何變化:( –

+0

在該文件中,你嘗試呢?您在編寫此代碼時是否加載了backbone和jquery文件? –

+0

它在我的footer.php,JavaScript代碼的其餘部分中的header.php –