Is it possible to generate the umbraco menu and insert some anchor links in there dynamically?
I have some pages and content that are part of a one page design such as this
url.com/what-we-do#section1
I'd like to include those anchor links in the drop down menu that is generated with the umbraco navigation macro.
I thought if i altered the macro code to try and add these in it could work but my vb and razor knowledge are bare minimum and i just receive run time errors.
heya,
for starter i would like to suggest using umbraco inbuilt snippets.
you can create one for navigation in creating a partial view using a snippet "navigation" which will give you below code. And yes this creates dynamic <a> tag based on number of pages u have in umbraco for level one.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@*
This snippet displays a list of links of the pages immediately under the top-most page in the content tree.
This is the home page for a standard website.
It also highlights the current active page/section in the navigation with the css class "current".
*@
@{ var selection = CurrentPage.Site().Children.Where("Visible"); }
<ul>
@foreach (var item in selection)
{
<li class="@(item.IsAncestorOrSelf(CurrentPage) ? "current" : null)">
<a href="@item.Url">@item.Name</a>
</li>
}
</ul>
Appreciate your help but i dont know if this will allow me to achieve what i am trying.
I've created a partial view which has pulled through a list of my pages which is great. The problem i have is that i have 1 part of that menu that i need to use links like http://domain.com/services#service-1
I have a services page which is populated in my nav menu and initially the content on this page was hosted on individual pages. We have changed this to contain all that content on one page. What i am struggling to do now is insert the links to these anchors within my main menu.
Really, what i need to do is insert something like this:
Hey Philb, can you show the code that generates the service page, especially the part where you set the anchors? I have an idea on how you can add the anchors to the menu but I'm not sure if it will work in your setup
The navigation itself is made with the sample that comes with the Fanoe kit (i think)
Menu:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{ var home = CurrentPage.Site(); }
@if (home.Children.Any())
{
@* Get the first page in the children *@
var naviLevel = home.Children.First().Level;
@* Add in level for a CSS hook *@
<ul class="level-@naviLevel">
@* For each child page under the home node *@
@foreach (var childPage in home.Children)
{
if (childPage.Children.Any())
{
<li class="has-child @(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">
@if(childPage.DocumentTypeAlias == "LandingPage")
{
<span>@childPage.Name <img src="/media/1113/triangle-down.png" alt="down-arrow" style="height:14px;"></span>
@childPages(childPage.Children)
} else {
<a href="@childPage.Url">@childPage.Name</a>
}
</li>
}
else
{
<li class="@(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">
<a href="@childPage.Url">@childPage.Name</a>
</li>
}
}
</ul>
}
@helper childPages(dynamic pages)
{
@* Ensure that we have a collection of pages *@
if (pages.Any())
{
@* Get the first page in pages and get the level *@
var naviLevel = pages.First().Level;
@* Add in level for a CSS hook *@
<ul class="sublevel level-@(naviLevel)">
@foreach (var page in pages)
{
<li>
<a href="@page.Url">@page.Name</a>
@* if the current page has any children *@
@if (page.Children.Any())
{
@* Call our helper to display the children *@
@childPages(page.Children)
}
</li>
}
</ul>
}
}
With WordPress I'd just register a menu then use the prebuilt menu builder. I tried to use a couple of the menu builder plugins for Umbraco but i couldnt get them to work.
Hi Marcio,
Thanks for that. I will give that a try this afternoon and report back,, however Sven i'd still like to hear your suggestion. Either way it is a learning experience.
Generate menu using anchor links
Hello,
Is it possible to generate the umbraco menu and insert some anchor links in there dynamically?
I have some pages and content that are part of a one page design such as this
url.com/what-we-do#section1
I'd like to include those anchor links in the drop down menu that is generated with the umbraco navigation macro.
I thought if i altered the macro code to try and add these in it could work but my vb and razor knowledge are bare minimum and i just receive run time errors.
Any help would be much appreciated.
Regards
PhilB
heya, for starter i would like to suggest using umbraco inbuilt snippets. you can create one for navigation in creating a partial view using a snippet "navigation" which will give you below code. And yes this creates dynamic
<a>
tag based on number of pages u have in umbraco for level one.Hi Bhawna, thanks for your reply.
Appreciate your help but i dont know if this will allow me to achieve what i am trying.
I've created a partial view which has pulled through a list of my pages which is great. The problem i have is that i have 1 part of that menu that i need to use links like http://domain.com/services#service-1
I have a services page which is populated in my nav menu and initially the content on this page was hosted on individual pages. We have changed this to contain all that content on one page. What i am struggling to do now is insert the links to these anchors within my main menu.
Really, what i need to do is insert something like this:
in the main navigation just for that one menu item. The rest of them can be generated based on the pages that are published.
The links point to Anchors in the Umbraco RTE content of the page.
I can see that the link is when i view the source. I need to use the link-id instead of looping through the pages.
I looked at using megaNav plugin from umbraco but couldnt get it to work and would prefer to use as little plugins/addons as possible.
Thanks,
PhilB
Hey Philb, can you show the code that generates the service page, especially the part where you set the anchors? I have an idea on how you can add the anchors to the menu but I'm not sure if it will work in your setup
Hi Sven,
Sorry the delay in getting back to you. I was away all weekend.
do you mean the template HTML itself?
Thanks,
Phil
Yup, that is what I mean
For dynamic menu with anchor, I do this:
You can use the ID, umbracoUrlName or Name
Hi,
Apologies for the delay.
I'm using a master template here:
The navigation itself is made with the sample that comes with the Fanoe kit (i think)
Menu:
With WordPress I'd just register a menu then use the prebuilt menu builder. I tried to use a couple of the menu builder plugins for Umbraco but i couldnt get them to work.
Hi Marcio,
Thanks for that. I will give that a try this afternoon and report back,, however Sven i'd still like to hear your suggestion. Either way it is a learning experience.
Thanks,
PhilB
Sorry for the late reply, was on Umbraco training the last few days.
I was going to suggest to do something like this ( from your nav script)
in combination with Marcio's answer. Just don't forget to update your helper script as well.
If you stumble upon any errors, just let me know. I think I understand your current setup ;)
is working on a reply...