javascript
  • jquery
  • blogger
  • 2017-08-25 67 views 2 likes 
    2

    我的目標是如果我的帖子在分享時包含特定的class/es,則需要進行特定的圖片預覽。如何根據div類分配元標記圖像內容url值?

    我正在使用博客平臺及其默認代碼。

    <b:if cond='data:post.labels any (l =&gt; l.name in {"video", "youtube"})'> 
        <meta content='post-with-video-class.jpg' property='og:image'/> 
    
    <b:elseif cond='data:post.labels any (l =&gt; l.name in {"gallery"})'/> 
        <meta content='post-with-gallery-class.jpg' property='og:image'/> 
    
    <b:else/> 
        <meta content='default-class-url.jpg' property='og:image'/> 
    </b:if> 
    

    ...和我的內容

    <div id='posts'> 
        <div class='video and youtube'> 
         post 1 
        </div> 
        <div class='gallery'> 
         post 2 
        </div> 
        <div class='default'> 
         post 3 
        </div> 
    </div> 
    

    如果是body內,纔能有預覽圖像此代碼只會工作,meta標籤應該是裏面head

    我試圖追加jQuery,但仍然沒有預覽圖像。

    $("[property*='og:image']").appendTo ("head"); 
    

    是否有任何jQuery代碼來代替博客的代碼? 我正在閱讀classListhasClass,但執行它們仍然離我的知識還很遙遠。請幫忙。

    +1

    如果您要將這些標籤用於Facebook或任何其他社交媒體平臺,它將無法正常工作,因爲需要在加載頁面時創建元標記 –

    +0

    我有兩個問題:您是否想擁有爲帖子列表網頁或帖子詳細信息網頁設置的「og:image」?你使用哪個主題? –

    +0

    @winner_joiner我希望他們在兩種頁面類型。它從頭開始。 – 88willr

    回答

    1

    可能有更好的解決方案,但這裏有一個可行的解決方法,可行。
    在這個假設下:

    • 列表視圖,爲Blog一個默認圖像應該顯示。
    • 對於詳細視圖,將顯示視頻或圖庫圖像。
    • 郵政標題可以有一個關鍵字來過濾,而不是標籤。

    此代碼可以輸入到head -tag中,並創建指定的meta -tag。對於這個例子中我使用了關鍵字(視頻)post-with-video-class.jpg圖像和所述關鍵字(圖片)用於圖像post-with-gallery-class.jpg,在任何其他情況下,默認圖像將被顯示。
       
    任何其他關鍵字可以使用,但也限制了標題選項

    <b:with value='data:widgets.Blog.first.posts.first' var='post'> 
        <b:if cond='data:view.isSingleItem and data:post.title contains &quot;(video)&quot;'> 
         <meta content='post-with-video-class.jpg' property='og:image'/> 
        <b:elseif cond="data:view.isSingleItem and data:post.title contains &quot;(pics)&quot;"/> 
         <meta content='post-with-gallery-class.jpg' property='og:image'/> 
        <b:else/> 
         <meta content='default-class-url.jpg' property='og:image'/> 
        </b:if> 
    </b:with> 
    

    這不是優雅,但可能或多或少簡單的解決方法。

    提示:對於這項工作,應根據新的主題(如Contempo的因爲一些功能,標籤和數據元件不舊的主題可用。
       
    這裏是一些好的文檔,這可能有助於http://template-data.blogspot.co.at

    由於沒有太多的文檔,我不能說,如果labels屬性可以在data:widgets.Blog.first.posts.first後對象進行訪問。

    相關問題