Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matias Korn 30 posts 49 karma points
    Apr 17, 2012 @ 10:50
    Matias Korn
    0

    List News with category

    Hello everybody. I want to list all the news order by date. The thing is that i have a Content  structure that goes like this

    News

          -> [News Category]

                 -> [News Item]

     

    How can i list all the News items order by date and inside each news item show the image of the news category.

    I just want to know how to handle this - thanks

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Apr 17, 2012 @ 11:00
    Chriztian Steinmeier
    0

    Hi Matias,

    Inside a News Item (or in a for-each where the current item is a NewsItem) you can access the parent category with this:

    <xsl:variable name="myCategory" select="parent::NewsCategory" />

    - and then access the properties of the category as you usually do, e.g.:

    <xsl:value-of select="$myCategory/image" />

    (This assumes the Alias of the News Category document type is NewsCategory - change the code accordingly if it's called something else.)

    /Chriztian

  • Matias Korn 30 posts 49 karma points
    Apr 17, 2012 @ 11:02
    Matias Korn
    0

    And how do i list all the news? because usually it is $currentPage/* but would my xPath be something like /*/* then or what ? how do i list the news of all the categories and sorting them by date?

    And thanks for that! didn't knew that.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Apr 17, 2012 @ 11:10
    Chriztian Steinmeier
    0

    Hi Matias,

    Here's a good starting point:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
        <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
        <!-- Adjust the XPath here for selecting the newsRoot based on the site root -->
        <xsl:variable name="newsRoot" select="$siteRoot/News" />
    
        <xsl:template match="/">
            <!-- Process all news items, regardless of depth -->
            <xsl:for-each select="$newsRoot//NewsItem">
                <xsl:sort select="@createDate" data-type="text" order="descending" />
    
                <!-- Handle the News Item -->
                <xsl:apply-templates select="." />
    
            </xsl:for-each>
        </xsl:template>
    
        <!-- Template for a News Item -->
        <xsl:template match="NewsItem">
            <xsl:variable name="myCategory" select="parent::NewsCategory" />
            <p>
                "<xsl:value-of select="@nodeName" />", category: <xsl:value-of select="$myCategory/@nodeName" />
            </p>
        </xsl:template>
    
    </xsl:stylesheet>

    /Chriztian

  • Matias Korn 30 posts 49 karma points
    Apr 17, 2012 @ 11:47
    Matias Korn
    0


    <xsl:stylesheet
            version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:umb="urn:umbraco.library"
            exclude-result-prefixes="umb">

            <xsl:output method="html" indent="yes" omit-xml-declaration="yes" />

            <xsl:param name="currentPage" />
            <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
            
            
            <xsl:variable name="newsRoot" select="$siteRoot/Home/News" />
            
            <xsl:template match="/">
                    
                    <xsl:for-each select="$newsRoot//NewsItem">
                            <xsl:sort select="@createDate" data-type="text" order="descending" />
                      
                            
                            <xsl:apply-templates select="." />
                    xsl:for-each>
            xsl:template>
            
            
            <xsl:template match="NewsItem">
              <xsl:variable name="myCategory" select="parent::NewsCategory" />
              <div class="home-news">
                <div class="home-news-catimg">
                  <img>
                    <xsl:attribute name="src">
                      <xsl:value-of select="$myCategory/newsCategoryImg" />
                    xsl:attribute>
                  img>
                div>
                <a>
                  <xsl:attribute name="href">
                    <xsl:value-of select="@urlName"/>
                  xsl:attribute>
                  <h2 class="home-news-title"><xsl:value-of select="@nodeName" />h2>
                a>
                <div class="home-news-content">
                  <xsl:value-of select="newsCategoryItemContent" disable-output-escaping="yes"/>
                  <a class="home-news-readmore">
                    Read more
                    <xsl:attribute name="href">
                      <xsl:value-of select="@urlName"/>
                    xsl:attribute>
                  a>
                div>
                <div class="home-news-line">div>
              div>
            xsl:template>

    xsl:stylesheet>


    It doesn't show anything. I think it cant find the news items.

    I have checked all the names is correct and the content tree is like this:

    Content -> Home -> [News Root] -> [News Category] -> [News Item]

    And again thanks for the help,  i dont know what i am doing wrong...

    I have also tried with $siteRoot/News

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Apr 17, 2012 @ 11:55
    Chriztian Steinmeier
    0

    Hi Matias,

    It's most likely an issue with the aliases - XPath needs the "nodeTypeAlias", not the "nodeName" - so be sure to double check that the Home and News nodes have those aliases.

    /Chriztian

     

  • Matias Korn 30 posts 49 karma points
    Apr 17, 2012 @ 12:03
    Matias Korn
    0

    yes and we both aggree that it should be that Alias of the document type right?

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Apr 17, 2012 @ 12:26
    Chriztian Steinmeier
    0

    Yes - the same goes for NewsItem and NewsCategory of course...

    BTW: Which version of Umbraco are you using? 

    /Chriztian

  • Matias Korn 30 posts 49 karma points
    Apr 17, 2012 @ 12:31
    Matias Korn
    0

    I am using umbraco 4.7

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Apr 17, 2012 @ 12:38
    Chriztian Steinmeier
    0

    OK - hardcore methods to the rescue - use this root template and let's see if the results makes sense:

    <xsl:template match="/">
        <textarea rows="8" cols="40">
            <xsl:copy-of select="$currentPage/ancestor-or-self::*[@level = 1]" />
        </textarea>
    </xsl:template>

     

    If you can paste some of it (not it all - it will dump all the XML for the site), I'll have a look at what could be wrong.

    /Chriztian

  • Matias Korn 30 posts 49 karma points
    Apr 18, 2012 @ 00:35
    Matias Korn
    0

    i did what you wrote and tested my way on and now it is working and the problem in the end was a xsl:attribute that contained a xsl:value of that returned @urlName that somehow returned an error.

    I only have a problem now with href="{umbraco.library:NiceUrl(@id)}" that it gives an error that doesn't make any sense.

    i fixed the problem with the @urlName by wrapping the text inside that a with xsl:text

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Apr 18, 2012 @ 00:43
    Chriztian Steinmeier
    0

    Hi Matias,

    Good to hear you've got it working.

    If you're still using my code from the beginning, you're probably seeing an error along the lines of "prefix not defined" or similar. I like to shorten the umbraco.library: prefix to just umb, so you should use this:

    <a href="{umb:NiceUrl(@id)}" />

    instead...

    /Chriztian

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Apr 18, 2012 @ 00:47
    Chriztian Steinmeier
    0

    Also - as long as you're just outputting simple values to attributes, you can just use the curly braces instead - makes it much easier to read (and you don't have to make sure you're not outputing any text before trying to do the attribute), so:

    <a>
        <xsl:attribute name="href">
            <xsl:value-of select="@urlName" />
        </xsl:attribute>
        Link
    </a>

    Is much easier written:

    <a href="{@urlName}">Link</a>

     

    /Chriztian

  • Matias Korn 30 posts 49 karma points
    Apr 19, 2012 @ 10:21
    Matias Korn
    0

    Thanks for the advice man! :) i am realy happy for the help! :)

  • Matias Korn 30 posts 49 karma points
    Apr 26, 2012 @ 12:41
    Matias Korn
    0

    I have a problem with my list and that is that it wont get the new "news" results. It feels like it wont remove the cache, but i have tried to change something in web.config and then back, but it wont update with the new changes. The old news that is delete, is keep staying there.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Apr 26, 2012 @ 12:46
    Chriztian Steinmeier
    0

    Definitely sounds like a cache problem - does the old (deleted) News appear in the App_Data/Umbraco.config XML file? Does the newly created News appear in that file?

    Which settings have you got on the News macro for the "Cache Period ____ Seconds" setting?

    /Chriztian 

  • Matias Korn 30 posts 49 karma points
    Apr 26, 2012 @ 12:52
    Matias Korn
    0

    Yes the old news is delete in umbraco.config and the new news is there and cache periode is 0 seconds

  • Matias Korn 30 posts 49 karma points
    Apr 30, 2012 @ 08:38
    Matias Korn
    0

    is there anybody that have any idea about the cache problem??

Please Sign in or register to post replies

Write your reply to:

Draft