Copied to clipboard

Flag this post as spam?

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


  • Lucy Ryder 2 posts 22 karma points
    Feb 03, 2014 @ 14:08
    Lucy Ryder
    0

    Output class on <body> tag from page ID / name

    Hello, I'm new to Umbraco, and woudl like ot know how to output the page id / name as a class on the <body> tag on each page of my site so i can apply different CSS to some pages.

     

    I have read a few of ther similar posts on here, but i'm not sure where the different code goes - pls coud someone help!

    If you can tell me which files to put the lines of code that would be amazing, just getting used to the structure of files after moving form usding Wordpress!

     

    Thanks in advance,

    Lucy

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 03, 2014 @ 15:23
    Dennis Aaen
    1

    Hi Lucy,

    One way you could do output page ID / name in a class in the body tag, is by using umbraco item-element http://our.umbraco.org/wiki/reference/templates/umbracoitem-element

    So if you have a setup with template inheritance, go to the developer section, under templates, find your body tag an add class, after that you can output the pageID or page name by usig this.

    This gives you the pageID in the class

     <body class="<umbraco:Item field="pageID" runat="server" />">

    And the line here will give you the name of the page

     <body class="<umbraco:Item field="pageName" runat="server" />"> 

    or the id of the template that the page are using.

     <body class="<umbraco:Item field="template" runat="server" />"> 

    I hope this make sense to you if not just keep asking and I will try to help you.

    /Dennis

     

  • Lucy Ryder 2 posts 22 karma points
    Feb 04, 2014 @ 16:33
    Lucy Ryder
    0

    Thank very much for this Dennis! - will try this now :)

  • Funka! 398 posts 661 karma points
    Feb 04, 2014 @ 23:14
    Funka!
    1

    I'd recommend prefixing the pageID with some letters, since a CSS class name cannot (should not!) start with a number. So for example, a simple correction to Dennis's first example,

    <body class="pageid-<umbraco:Item field="pageID" runat="server" />">
    

    Good luck!

Please Sign in or register to post replies

Write your reply to:

Draft