Reputation: 21
I have four home nodes for various cultures and host names which are all level 1 in my content tree.
I'm looking to check if the current page exists as a child item in either of those nodes and if so display links, specifically an hreflang tag. (not all child pages exist on every node)
What I've tried so far is creating foreach loops to loop through and it's child items, however so far it only returns me the children for level 1.
IPublishedContent homeOne = Umbraco.TypedContent(1172);
IPublishedContent homeTwo = Umbraco.TypedContent(6093);
IPublishedContent homeThree = Umbraco.TypedContent(7886);
IPublishedContent homeFour = Umbraco.TypedContent(9679);
var pageUrl = CurrentPage.Url().ToString();
foreach (var item in homeOne.Children)
{
var pagePath = item.Url.ToString();
if (pagePath == pageUrl)
{
<link rel="alternate" href="@string.Format("https://www.domain.xx{0}", pagePath)" hreflang="x-default" />
<link rel="alternate" href="@string.Format("https://www.domain.xx{0}", pagePath)" hreflang="en-gb" />
}
}
I then have three similar foreach loops beneath the example above for the other home nodes, interestingly/annoyingly...if I'm on a child page of homeOne, I only see the links generated for homeOne, however if I go to a child page of homeTwo I see the links generated for homeOne and homeTwo. Subsequently visiting homeThree returns links for homeOne and homeThree only.
edit
I should re-iterate that these four root nodes have already set up with cultures and host names and are all in the same language. I'm basically just trying to look at what the current page is, if it exists as a child item across either or all of the four root nodes then show the relevant links
Current Tree Example-
Hope someone can point me in the right direction
Thanks
Upvotes: 0
Views: 412
Reputation: 21
The final solution I found for this was provided via an Umbraco community member, I made some tweaks to the final solution which are documented at the foot of the post below
Upvotes: 0
Reputation: 3425
CurrentPage should have a Path property which contains the IDs of every ancestor. I believe it's a string that you can split by ',' and then see if the resulting array contains either of the homeNode IDs. Something like this: https://coderwall.com/p/gl7poa/active-navigation-in-umbraco
From your code example I can't really figure out what you're trying to do, though. If you are trying to run a classic multilingual setup, have a look at this tutorial - setting it up like this (with domains/hostnames per language) lets Umbraco take care of a lot of the hassle: https://our.umbraco.com/Documentation/Tutorials/Multilanguage-Setup/index-v7
Upvotes: 0