I'm trying to create a footer navigation using the MultiNode Tree Picker. I want the user to be able to select page's on the homepage document type and show it on all page's.
I watched the video Insert umbraco page field dialog from umbraco.tv.
This works great for a text string.
So if your picker content is only set on the homepage then you'll need to query against the homepage to get that content. At the moment you are using Model.Content which queries against the current page.
Depending on what version of Umbraco you have installed you can either use (for newer version):
var footerNav = Model.Content.Site().GetPropertyValue<IEnumerable<IPublishedContent>>("footerNav");
For older version you'll need to use
var footerNav = Model.Content.AncestorOrSelf(1).GetPropertyValue<IEnumerable<IPublishedContent>>("footerNav");
Site() is just an extension method that does the same as the second example. What they do is go to the root ancestor of your site (the homepage) and query the data from there.
You can then put this in your master template and it should shown on every page. You can find more about querying content in the docs.
I usually use the "related links" datatype for this kind of stuff, as it then allows the user to mix up external links, linkTargets, and set an alternative caption/title.
But a MNTP works just fine, if you want to contrain it to just umbraco content nodes.
Got another question for you guru's.
I also want to be able to add a logo with a url with related links or link picker. I created an Archetype with 2 properties:
logo (media picker)
link (related links).
I'm trying the same trick as provided above:
@{
var footerLogos = Model.Content.Site().GetPropertyValue<IEnumerable<IPublishedContent>>("footerLogos");
@foreach (var item in footerLogos)
{
<a href="@item.GetPropertyValue("link")">
<img src="@item.GetPropertyValue("logo")"/>
</a>
}
}
I've not used RelatedLinks datatype much, but code looks fine to me. The only thing I'd change is doing a null-check on logo. You may get occasions where the associated image has been deleted or something that can cause it not to exist, then you'll be accessing the Url property on a null reference and it will go "booom!" :)
So something like this would avoid that:
var logo = fieldset.GetValue<IPublishedContent>("logo");
if (logo != null)
{
foreach (var relatedLink in relatedLinks)
{
var linkTarget = (relatedLink.NewWindow) ? "_blank" : null;
<a href="@relatedLink.Link" target="@linkTarget">
<img src="@logo" alt="@relatedLink.Caption"/>
</a>
}
}
Or you could fallback to a default image or a non-image link in those (hopefully rare) instances.
Multinode Tree Picker for footer navigation
Hi all,
I'm trying to create a footer navigation using the MultiNode Tree Picker. I want the user to be able to select page's on the homepage document type and show it on all page's. I watched the video Insert umbraco page field dialog from umbraco.tv. This works great for a text string.
The code I currently have is:
footerTitle is showing on all pages but how can I show the footerNav items on all pages?
Thank you in advance.
So if your picker content is only set on the homepage then you'll need to query against the homepage to get that content. At the moment you are using
Model.Content
which queries against the current page.Depending on what version of Umbraco you have installed you can either use (for newer version):
For older version you'll need to use
Site()
is just an extension method that does the same as the second example. What they do is go to the root ancestor of your site (the homepage) and query the data from there.You can then put this in your master template and it should shown on every page. You can find more about querying content in the docs.
Thanks Dan!
works like a charm
Just what I was looking for. Thanks
Hi,
How I will use Multinode Tree Picker as recursive.
My scenario needs recursive footer items. I am using 7.6.6 version of umbraco.
Earlier I was using - var mainNavigationItems = Umbraco.Field("mainNavigationItems", altText: "0", recursive: true).ToString().Split(',');
I usually use the "related links" datatype for this kind of stuff, as it then allows the user to mix up external links, linkTargets, and set an alternative caption/title.
But a MNTP works just fine, if you want to contrain it to just umbraco content nodes.
Got another question for you guru's. I also want to be able to add a logo with a url with related links or link picker. I created an Archetype with 2 properties:
I'm trying the same trick as provided above:
But when it renders it give me this back:
Is there something I'm not seeing?
Hope you guys can help me out!
@item.GetPropertyValue("logo")
Swap to...
@Umbraco.TypedMedia(item.GetPropertyValue("logo")).Url
That should give you a clue for your link problem too ;-)
Unfortunately this doesn't work. The code I'm using is
But it says Object reference not set to an instance of an object. See provided screenshot
Well I got it working now... This is the code I ended up using
It's working but is this the way to go?
I've not used RelatedLinks datatype much, but code looks fine to me. The only thing I'd change is doing a null-check on logo. You may get occasions where the associated image has been deleted or something that can cause it not to exist, then you'll be accessing the Url property on a null reference and it will go "booom!" :)
So something like this would avoid that:
Or you could fallback to a default image or a non-image link in those (hopefully rare) instances.
is working on a reply...