简体   繁体   中英

TYPO3 first available image of content elements

I want to show only the first available image of different content elements in a column.

The following code shows me all available images, but I need only the first one! Can somebody help me?

lib.contentImage = CONTENT
lib.contentImage {
  wrap = |
  table = tt_content
  select {
    languageField = sys_language_uid
    where = colPos = 0
    orderBy = sorting
    pidInList = 32
  }
  renderObj = COA
  renderObj{
    wrap = <div class="item">|</div>
    10 = FILES
    10 {
        references {
            table = tt_content
            uid.data = uid
            fieldName = assets
        }
        renderObj = IMAGE
        renderObj {
            wrap = <div class="item-image">|</div>
            file.import.data = file:current:originalUid
            file.width = 1920c
            file.height = 600c
        }
    }
  }
}

I used this to get the url of the first image on a page for open graph tags.

page.meta.og:image.cObject = CONTENT
page.meta.og:image.cObject {
    table = tt_content
    select {
        where = (colPos = 0 AND image != 0 )
        selectFields = uid
        orderBy = sorting
        max = 1
    }
    renderObj = COA
    renderObj {
        1 = TEXT
        1 {
            cObject = FILES
            cObject {
                references {
                    table = tt_content
                    uid.field = uid
                    fieldName = image
                }
                maxItems = 1
                renderObj = TEXT
                renderObj {
                    typolink.parameter.data = file:current:publicUrl
                    typolink.forceAbsoluteUrl = 1
                    typolink.returnLast = url
                }
            }
        }
    }
}

You would try somethings with LOAD_REGISTER

eg: (not tested)

lib.contentImage = COA
lib.contentImage {
  5 = LOAD_REGISTER
  5.imageRendered = 0

  10 = CONTENT
  10 {
    wrap = |
    table = tt_content
    select {
      languageField = sys_language_uid
      where = colPos = 0
      orderBy = sorting
      pidInList = 32
    }
    renderObj = COA
    renderObj{
      wrap = <div class="item">|</div>
      10 = FILES
      10 {
        references {
          table = tt_content
          uid.data = uid
          fieldName = assets
        }

        max = 1

        renderObj = COA
        renderObj {
          stdWrap.if.isFalse.data = register:imageRendered

          5 = LOAD_REGISTER
          5.imageRendered = 1

          10 = IMAGE
          10 {
            wrap = <div class="item-image">|</div>
            file.import.data = file:current:originalUid
            file.width = 1920c
            file.height = 600c
          }
        }
      }
    }
  }

  99 = RESTORE_REGISTER
}

Do you need the first one of each Contentelement or the first Image of all Contentelements?

If you only want one image for each element you can set maxItems in FILES Object https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Files/Index.html#usage-with-references

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM