Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jesper Skjønnemand 66 posts 441 karma points c-trib
    May 28, 2019 @ 10:09
    Jesper Skjønnemand
    0

    How to use property value of current page in tag query

    Hello

    I am attempting to create a template that displays a list of all nodes tagged with the name of the current page, e.g. display all nodes tagged Odense on a page named Odense.

    If I just enter GetContentByTag("Odense") the page displays name and link of two nodes as expected.

    But when I insert a variable fetching the pageTitle of the current page in place of Odense, no nodes are displayed.

    So far, the template looks like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.List>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @{
        Layout = "Main.cshtml";
    }
    
    @{
        var thetag = Model.Content.GetPropertyValue("pageTitle");
        var result = Umbraco.TagQuery.GetContentByTag("@thetag");
    }
    
    @foreach(var item in result)
        {
        <p><a href="@item.Url">@item.Name</a><br/>@Umbraco.Field(item, "personFirstName")</p>
        }
    

    What am I doing wrong?

    Probably some rookie mistake ...

  • Paul Tilsed 26 posts 179 karma points c-trib
    May 30, 2019 @ 11:04
    Paul Tilsed
    0

    Hi Jesper,

    It might be worth checking "pageTitle" is the property you really want to retrieve. That's not a default property value in Umbraco so you would need to add it to the document type first.

    If you are just after the name of the node then you could use

     var thetag = Model.Content.Name;
    

    Paul

  • Jesper Skjønnemand 66 posts 441 karma points c-trib
    Jun 03, 2019 @ 07:21
    Jesper Skjønnemand
    0

    Tried with Model.Content.Name ... same result.

    Weird thing is that

    1. <p><strong>@thetag</strong></p> renders Odense on my page
    2. var result = Umbraco.TagQuery.GetContentByTag("Odense"); renders a list of nodes tagged Odense.
    3. var result = Umbraco.TagQuery.GetContentByTag("@thetag"); renders nothing, not even an error message.
  • Jesper Skjønnemand 66 posts 441 karma points c-trib
    Jun 04, 2019 @ 12:05
    Jesper Skjønnemand
    100

    sometimes little things make a big difference ... this combo works as intended:

    var thetag = Model.Content.GetPropertyValue<string>("pageTitle");
    var result = Umbraco.TagQuery.GetContentByTag(@thetag);
    
  • Jesper Skjønnemand 66 posts 441 karma points c-trib
    Jun 04, 2019 @ 12:06
    Jesper Skjønnemand
    0

    basically missed the <string> ... rookie mistake, I know :-)

Please Sign in or register to post replies

Write your reply to:

Draft