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'.
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.
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" />
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.
(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!
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'. 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!
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
or
Option 2 - recursive property
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
Bravo Marc!
I did have to edit your code slightly for the first option, see else statement
Hi Mark
(ahh yes Root is the new syntax, I think it is 'Site' in your version of Umbraco)
so
which is just a convenience for:
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
Actually in 8 its extremely simple:
Yes, @claushingebjerge.... but this is V7... recursively accessing the property is actually also extremely simple in V7 :-P - (Option 2 above)
eg
Oh sorry, didnt see it was 7. Well theres an answer for both 7 and 8 now :)
is working on a reply...