Copied to clipboard

Flag this post as spam?

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


  • BarneyHall 141 posts 210 karma points
    Jun 02, 2010 @ 21:03
    BarneyHall
    0

    Count number of posts with category selected

    Hi, I've been hacking around the blog4umbraco package.

    Instead of tags, I created a checkbox list using the Ultimate Picker to define a list of categories. When you create a post you just select from the list categories what you think your post falls under. This is then stored within a data node as such:

    <data alias="categories"><![CDATA[1108]]></data>

    I've created a short list of category links for my homepage, but I also wanted to display the number of posts each category has.

    Could anyone offer any help with this?

    I thought it might be something like this since the categories are stored as IDs.

    <xsl:value-of select="count($blogRoot/ancestor-or-self::node [@nodeTypeAlias = 'Blog']//node [@nodeTypeAlias = 'BlogPost' and contains(Exslt.ExsltStrings:lowercase(./data [@alias='categories']), Exslt.ExsltStrings:lowercase(./@id))])"/>

    My heads spinning, I'm not even completely sure what this is really doing but it gives me a value of 0, which isn't right.

    Maybe there's an easier way to do this that I'm missing?

    Many thanks,
    Barney

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jun 02, 2010 @ 22:27
    Chriztian Steinmeier
    1

    Hi Barney,

    (I don't get why you're lowercasing numbers here - but maybe this has been modified a little from another use case?)

    Anyway, first make sure that you can count all blogposts under the $blogRoot:

    <xsl:value-of select="count($blogRoot//node[@nodeTypeAlias = 'BlogPost'])" />

    The double-slash is a little hard on the prcessor, so if 'BlogRoot' only allows 'Blog' as children and 'Blog' only allows 'BlogPost' children, you can be more specific (and efficient):

    <xsl:value-of select="count($blogRoot/node[@nodeTypeAlias = 'Blog']/node[@nodeTypeAlias = 'BlogPost'])" />

    Okay - then tack on the contains() stuff (I'll take the shorter one here):

    <xsl:value-of select="count($blogRoot//node[@nodeTypeAlias = 'BlogPost'][contains(data[@alias = 'categories'], current()/@id)])" />

    It's important to use current()/@id and not "./" here, because you're referring to the node outside of the value-of statement...

    This will come back with wrong counts if an id is contained within the id of another, e.g. 110 and 1101, so we should fix that with a common little trick: Tack a comma (assuming the categories property is a comma-separated list of ids) on both sides of the id so you're checking for the appearance of ",110," instead:

    <xsl:value-of select="count($blogRoot//node[@nodeTypeAlias = 'BlogPost'][contains(concat(',', data[@alias = 'categories'], ','), concat(',', current()/@id, ','))])" />

     

    /Chriztian

  • BarneyHall 141 posts 210 karma points
    Jun 03, 2010 @ 00:48
    BarneyHall
    0

    Hi Chriztian, this is top dollar, thank you.

    Using your first check I found that the variable I was using wasn't working as I wasn't getting a count of any posts.

    So I just replaced the $blogRoot variable with the full path:

    $currentPage/ancestor-or-self::node [@nodeTypeAlias = 'Blog']

    And it's worked beautifully.

    Not sure why that variable wasn't working, it had the same path as its value?

    XSLT is still martian to me, so it's not hard for me to get confused.

    Thanks again for taking the time to help me.

    Best regards,
    Barney

     

     

Please Sign in or register to post replies

Write your reply to:

Draft