Copied to clipboard

Flag this post as spam?

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


  • Mark 14 posts 84 karma points
    Apr 23, 2020 @ 04:51
    Mark
    0

    Inserting custom meta description tag on each page.

    Hello community,

    I am trying to create a custom meta description for each page on my Umbraco website. I want to add a doc type to my page settings called 'Custom Meta Description'. enter image description here The home page will always have a meta Description but other pages like the about page may or may not. In the event that another page does not have a Custom Meta Description declared I would like to use the Custom Meta Description that is on the home page.

    I am very new to Umbraco and razor code so if you could leave me some detailed information on how to achieve this I would be very grateful.

    Thank you in advance!

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Apr 23, 2020 @ 06:20
    Marc Goodson
    0

    Hi Mark

    Within each page template you could either check to see if a value for your custom meta description property exists on the page and if not get a reference to the homepage content item and read the fallback value from there, or if you might want to fall back to the page directly above for the value, and then if that is blank the page above that, all the way back up to the homepage, then you can write out the property with 'recurse' set to be true... and Umbraco will do that for you!

    Option 1 - check if value exists

    @{
    var customMetaDescription = "";
    if (Model.Content.HasValue("customMetaDescription"){
        customMetaDescription = Model.Content.GetPropertyValue<string>("customMetaDescription");
    }
    else {
        var homePage = Model.Content.Root;
        customMetaDescription = homepage.Content.GetPropertyValue<string>("customMetaDescription");
    }
    }
    <meta name="description" value="@customMetaDescription" />
    

    or

    Option 2 - recursive property

    <meta name="description" value="@(Model.Content.GetPropertyValue<string>("customMetaDescription",true))" />
    

    so much less type in Option2! but if you have a page within a page within a page, it will get the value from it's ancestors and not necessarily the homepage but that might be a better more relevant metadescription.

    regards

    Marc

  • Mark 14 posts 84 karma points
    Apr 23, 2020 @ 13:41
    Mark
    0

    Bravo Marc!

    I did have to edit your code slightly for the first option, see else statement

    @{
        var customMetaDescription = "";
        if (Model.Content.HasValue("customMetaDescription")){
          customMetaDescription = Model.Content.GetPropertyValue<string>("customMetaDescription");
        }
        else {
          var homePage = Model.Content.Parent;
          customMetaDescription = homePage.GetPropertyValue<string>("customMetaDescription");
        }
    }
    
  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Apr 24, 2020 @ 14:57
    Marc Goodson
    0

    Hi Mark

    (ahh yes Root is the new syntax, I think it is 'Site' in your version of Umbraco)

    so

    var homePage = Model.Content.Site;
    

    which is just a convenience for:

    var homePage = Model.Content.AncestorOrSelf(1);
    

    Parent will only read from the page above, which from your screenshot is probably fine, but if you ever create a page beneath a page, then using either of the above, should still find the homepage!

    regards

    Marc

  • Claushingebjerg 936 posts 2571 karma points
    Apr 24, 2020 @ 17:20
    Claushingebjerg
    0

    Actually in 8 its extremely simple:

    <meta name="description" value="@Model.Value("customMetaDescription", fallback: Fallback.ToAncestors)" />
    
  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Apr 24, 2020 @ 18:14
    Marc Goodson
    1

    Yes, @claushingebjerge.... but this is V7... recursively accessing the property is actually also extremely simple in V7 :-P - (Option 2 above)

    eg

    <meta name="description" value="@(Model.Content.GetPropertyValue<string>("customMetaDescription",true))" />
    
  • Claushingebjerg 936 posts 2571 karma points
    Apr 25, 2020 @ 07:13
    Claushingebjerg
    0

    Oh sorry, didnt see it was 7. Well theres an answer for both 7 and 8 now :)

Please Sign in or register to post replies

Write your reply to:

Draft