I have a header which has a background image which is set on the homepage, but you can set the header image per page. If a page doesnt have a header image set, it inherits from the homepage media picker value.
Here is it working where a page inherits from the homepage.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
int imageId = Umbraco.AssignedContentItem.GetPropertyValue<int>("topBannerImage");
string topBannerImage = imageId > 0 ? Umbraco.Media(imageId).Url : string.Empty;
if (string.IsNullOrWhiteSpace(topBannerImage))
{
var homePageImage = Umbraco.AssignedContentItem.Ancestor(1).GetPropertyValue<IPublishedContent>("topBannerImage").Url;
topBannerImage = homePageImage;
}
}
What I would like to add in, is I want it to check the parent page first and see if that parent page has an image. If it does, use it, else use the homepage.
This is what I have trying to get the parent image:
Based on the way you described the functionality, have you thought about going down the path of utilizing MVC Sections?
The only potential drawback that I immediately see is that you may have to provide a property for multiple doctypes. That will largely depend on how your site is structured. If the image is part of a composite used for every webpage, it might be less of an issue.
Below is some code which should allow you to use @FallBackimg.src to retrieve a path, but it uses an ImageCropper as opposed to a Media Type:
Is there a way to extend this so if the parent doesnt have an image, go up to the next parent and check there? then keep checking the parents until it reaches the root? so if no parents have an image set, use the root image?
Page Header Image
Could anyone help with the following:
I have a header which has a background image which is set on the homepage, but you can set the header image per page. If a page doesnt have a header image set, it inherits from the homepage media picker value.
Here is it working where a page inherits from the homepage.
}
What I would like to add in, is I want it to check the parent page first and see if that parent page has an image. If it does, use it, else use the homepage.
This is what I have trying to get the parent image:
}
The above is not working with getting the parent image. Can anyone see where I am going wrong? Any help on this would be much apprenticed.
Based on the way you described the functionality, have you thought about going down the path of utilizing MVC Sections?
The only potential drawback that I immediately see is that you may have to provide a property for multiple doctypes. That will largely depend on how your site is structured. If the image is part of a composite used for every webpage, it might be less of an issue.
Below is some code which should allow you to use @FallBackimg.src to retrieve a path, but it uses an ImageCropper as opposed to a Media Type:
Hi David
When you say it's not working, what error are you getting?
One possible issue is that for the parent page you are not checking there is an image assigned before trying to get the Url for the image.
So maybe change this line
to
Hope this helps
Nigel
thanks for this nigel. worked like a charm.
Is there a way to extend this so if the parent doesnt have an image, go up to the next parent and check there? then keep checking the parents until it reaches the root? so if no parents have an image set, use the root image?
Hi David
Pleased it solved the issue.
There is nothing I immediately know of with respect to automatic recursion for an IPublishedContent object.
You may have to write your own helper method, however someone else might be able to provide some ideas
Cheers
Nigel
is working on a reply...