Skip to main content

Posts

Showing posts from 2020

Umbraco 8 Pagination

Umbraco 8 Pagination / Paging:

@inherits Umbraco.Web.Mvc.UmbracoViewPage

@{
    Layout = "master.cshtml";

    var pageSize = 8;

    if(Model.HasValue("numberOfItemsPerPage")){
    pageSize = Model.Value<int>("numberOfItemsPerPage");}

    var page = 1; int.TryParse(Request.QueryString["page"], out page);

    @* This line assumes you have a property called lastUpdated*@
    var items = Model.Children().OrderByDescending(x => x.Value("lastUpdated"));
    @* Remove the line above and use this line to keep it simple *@
    @* var items = Model.Children()); *@


    var totalPages = (int)Math.Ceiling((double)items.Count() / (double)pageSize);

    if (page > totalPages)
    {
        page = totalPages;
    }
    else if (page < 1)
    {
        page = 1;
    }

}

@foreach (var item in items.Skip((page - 1) * pageSize).Take(pageSize))
            {
           <a href="@item.Url">@item.Name</a>
             }



Umbraco 8 OrderBy a collection by a Custom Field Such as a Date

This is a snippet for Umbraco 8  to Order a collection by a Custom Field Such as a Date.

@{
    var selection = Umbraco.Content(Model.Id)
    .ChildrenOfType("publication")
    .Where(x => x.IsVisible())
    .OrderBy( x => x.Value("publicationDate"));
}

@foreach (var item in selection.Take(1))
{
     <h2><a href="@item.Url" title="@item.Name">@item.Name</a></h2>
}

Umbraco 8 Metadata Snippet, keywords, meta title.

Umbraco 8 Metadata Snippet, keywords, meta title, meta description: The following is a snippet that may help with generating metadata. I created this as a partial and added it to the Main Template.

If the metadata fields are not filled in on the backend by the editor, they are generated from the Page Name and the Name of the Website (applicationName).

----

In the example the backend meta fields are called:
"metaTitle"   (textstring)
"metaDescription"   (textstring)
"metaKeywords" (tags)

----

@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
    var applicationName = "My Website Name";
}
@if(Model.HasValue("metaTitle")){
    <title>@Model.Value("metaTitle")</title>

else
{
    if(Model.Id == Umbraco.ContentAtRoot().FirstOrDefault().Id)
    {
        <title>@applicationName</title>
    }
    else
    {
        <title>@Model.Name | @applicationName</title>
    }
}


@if(Model.HasValue("metaDescription"…