(Umbraco) Can get text but not image from a subnode to homepage

与世无争的帅哥 提交于 2019-12-24 10:48:17

问题


I'm trying to get text and image from articles in the subsection "Featured" (Home/Featured/Article1, 2,...,N) but I don't get image. This is the code that works fine getting text from every article that is inside the 'Featured' Node.

 <xsl:if test="position() &lt; $maxItems">
     <h3><a href="{umbraco.library:NiceUrl(@id)}">
         <xsl:value-of select="newsTitle"/>
         </a>
     </h3>
     <strong><xsl:value-of select="intro"/></strong>

     <br/>      
     <small>
         A: <xsl:value-of select="umbraco.library:FormatDateTime($currentPage/@updateDate, 'MMMM d, yyyy')"/> 
         Por:  <xsl:value-of select="author"/>
     </small>
 </xsl:if>

It works just fine. But I cannot get images from an article. I'm trying this way, among others:

<a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:if test="count(./* [@isDoc]) > 0">
    <img src="{concat(substring-before(./*/thumbnail,'.'), '_thumb.jpg')}"/>
    </xsl:if>     
</a>

I don't know what to do here, I'm using 'Upload' property for an element and also with 'MediaPicker' (alias: 'thumbnail'), so, I've been testing with these property types but didn't get anything yet. I just want to put the image (if exists) of the article next to text retrieved from a childnode to homepage.

I'll appreciate your help. Thanks in advance!

[Umbraco 6.1.3]


回答1:


I think the error is that you need to select a property, not a node. You are using the @isDoc attribute, so no properties will be selected.

As mentioned, "thumbnail" is a property on a node (assuming only 1 thumbnail here) of datatype "Upload" (saves value of a path as a string):

<xsl:if test="string-length(./thumbnail) > 0">
<a href="{umbraco.library:NiceUrl(@id)}">
<img src="{concat(substring-before(./thumbnail,'.'), '_thumb.jpg')}"/>
</a>
</xsl:if>   

Or with the "media picker" datatype (saves the nodeid of the media-item as an int):

<xsl:if test="string-length(./thumbnail) > 0">
<xsl:variable name="image" select="umbraco.library:GetMedia(./thumbnail,0)"/>
<a href="{umbraco.library:NiceUrl(@id)}">
<img src="{concat(substring-before($image/umbracoFile,'.'), '_thumb.jpg')}"/>
</a>
</xsl:if>

Since the other code that is posted is working fine, and it is not using $currentPage, I assume that this code is wrapped in a template or you are iterating over a set of nodes.

I would also put the link element inside the if statement and check whether a thumbnail is available or not before creating a link, just to avoid empty link elements.




回答2:


In your case this should do the trick:

<a href="{umbraco.library:NiceUrl(@id)}">
  <xsl:if test="$currentPage/thumbnail != ''">
   <img src="{umbraco.library:GetMedia($currentPage/thumbnail, 0)/umbracoFile}"/>
  </xsl:if>     
</a>

See this page for reference




回答3:


here's an example:

<xsl:variable name="mediaId" select="number($currentPage/mediaId)" />
<xsl:if test="$mediaId > 0">
    <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
    <xsl:if test="$mediaNode/umbracoFile">
        <img src="{$mediaNode/umbracoFile}" alt="[image]" height="{umbracoHeight}" width="{umbracoWidth}" />
    </xsl:if>
</xsl:if>

How to use umbraco.library GetMedia in XSLT



来源:https://stackoverflow.com/questions/18154360/umbraco-can-get-text-but-not-image-from-a-subnode-to-homepage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!