首页 > 解决方案 > 如何在 tt_content 中为 meta og:image 获取第一张图片

问题描述

如果 tt_content 中有任何图像,我正在尝试获取 tt_content 中要在 Open Graph (og:image) 中使用的第一个现有图像。

page.headerData {
    1 = COA
    1 {
        10 = FILES
        10 {
            references {
                table = tt_content
                uid.field = uid
                fieldName = image
            }
            renderObj = TEXT
            renderObj {
                typolink {
                    parameter.data = file:current:publicUrl
                    forceAbsoluteUrl = 1
                    returnLast = url
                }
                wrap = |,
            }
            stdWrap {
                listNum = 0
                # Use logo image if none is available
                ifEmpty.cObject = TEXT
                ifEmpty.cObject.typolink {
                    parameter = typo3conf/ext/my_ext/Resources/Public/Images/logo.png
                    forceAbsoluteUrl = 1
                    returnLast = url
                }
                wrap = <meta property="og:image" content="|">
            }
        }
    }
}

上面的代码只返回后备(logo)。非常适合没有图像的页面和 tt_content,但我希望与 tt_content 中具有图像的页面不同。

标签: typo3typoscripttypo3-9.x

解决方案


我在这里报告在https://docs.typo3.org/m/typo3/docs-snippets/master/en-us/2014/Index.html#add-facebook-open-graph-og-image- metatags-for-content-elements-with-typo3-6-0-and-fal

我简化了一点;希望它不会过于简化,但它应该可以工作:

page.headerData.200 = CONTENT
page.headerData.200 {
  table = tt_content
  select {
    where = {#colPos}=0
    selectFields = uid
  }
  renderObj = FILES
  renderObj {
    references {
      table = tt_content
      uid.field = uid
      fieldName = image
    }
    maxItems = 1
    renderObj = TEXT
    renderObj {
      data = file:current:publicUrl
      stdWrap {
        wrap = <meta name="og:image" content="|" >
      }
    }
  }   
}

推荐阅读