2017-02-24 97 views
1

如何將模態圖像插入後插入的圖像WordPress當我創建一個帖子。如何把模態圖像插入圖像插入從WordPress的內容後?

我怎麼能控制在文章中插入圖片,並把自定義的的JavaScriptCSS我怎麼能做到這一點的WordPress的網站?

back-end pic

preview of the page

,但它不能訪問WordPress的

內容
$("#myModal .post_content img").each(function(){ 

    $("#myModal p").wrap('<div class="lightgallery"></div>'); 


    $(this).preprend('<a href=""> 
     <img data-sub-html="'.get_post(get_post_thumbnail_id())->post_excerpt.'" class="slider-image-fullscreen" data-src="'.get_the_post_thumbnail_url($postid,'full').'" src="'.get_template_directory_uri().'/images/Magnifier.png" style="position:absolute; height:25px; width:25px;"> 
     </a>'); 

}); 

enter image description here

+0

這東西,你可以很容易地通過安裝的WordPress插件 –

+0

我有同樣的問題做了,我解決了,但我想知道的是U使用的是什麼方法(modalbox或燈箱) – TriForce

+0

@TriForce - 如果你有同樣的方法和解決它可能是你應該在這裏分享,並幫助解決她的問題。 – codeepic

回答

1

好吧,u需要從WordPress的第一檢測圖像,我認爲u有他們在一個容器中,所以你需要在圖像容器中添加一個ID。 (也u可以使用一類或任何東西,但你需要識別圖像容器)的文件準備ü需要使用代碼這樣的事情在

//This is the container which contains the images inserted by wordpress 
$("#lightgallery img").each(function(){ //search all images inside 

    //get the imgs url 
    var imgSrc = $(this).attr("src"); 

    //put the image inside a div 
    $(this).wrap('<div class="myImage"></div>'); 

    //adds the button to launch the lightbox 
    $(this).parent().prepend('<a href="'+imgSrc+'">View</a>'); 

    //adds the button to launch the lightbox (include thumbnail) 
    //$(this).parent().prepend('<a href="'+imgSrc+'">View <img src="'+imgSrc+'"></a>'); 

}); 

//load gallery after images get converted to links 
$("#lightgallery").lightGallery({ //this is the parent container of imgs 
    selector:'a', //this is the button to launch the lightbox 
    thumbnail:false //if u want use thumbs change it to true, so u need include an image inside the button container to get detected as thumb, in this case inside the "a", u can "uncomment" the hidden line above to try it 
}); 

你可以看到完整的代碼我已經爲你做的(包括一個例子css)。

點擊這裏:https://jsfiddle.net/stptaj9h/23/

注意:不要忘了,包括所有的收藏庫(js和css),在這種情況下:lightgallery.css,lightgallery.js,LG-fullscreen.js和LG-thumbnail.js

+0

,但它不能訪問wordpress的內容 $(「#myModal .post_content IMG 「)每個(函數(){ $。(」 #myModal p「)包裹( '

'); $(本).preprend(' '); }); –

+0

1-您刪除了imgSrc變量,,因爲您需要將原始圖像源發送到鏈接按鈕,因此需要。 2-你不使用包裹在img中,應用於模態p ...這是錯誤的3-你寫prepRend而不是前置(不要雙R)。 4-你沒有在prepend()之前添加.parent()。你的代碼應該是這樣的:http://pastebin.com/tNs7xhA8記得閱讀我的例子:https://jsfiddle.net/stptaj9h/23/不要添加內聯類,只需從css中添加它們 – TriForce