Copied to clipboard

Flag this post as spam?

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


  • David Hyslop 27 posts 181 karma points
    Jan 05, 2016 @ 11:33
    David Hyslop
    0

    Hi

    I'm looking for a way to inherit a site logo from the homepage of my site.

    So I set the logo through the hompage and all the sub pages inherit the logo unless it's changed on one of the subpages. Anyone know a way of doing this.

    Thanks

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jan 05, 2016 @ 12:07
    Dennis Aaen
    0

    Hi David,

    If you are using the media picker to pick the logo for the website something like this should do it,

    If you are using dynamic Razor use the below code.

    @{      
        if (CurrentPage.HasValue("mainImage", true)){                                         
            var dynamicMediaItem = Umbraco.Media(CurrentPage.mainImage);
            <img src="@dynamicMediaItem.umbracoFile" alt="@dynamicMediaItem.Name"/>
        }
    }
    

    If you are using strongly typed Razor the code should look like this.

    @{
        if(Model.Content.HasValue("mainImage", true)){
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("mainImage")); 
            <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")"/>    
        }   
    }
    

    Remember to change the "mainImage" so it match your property alias for your media picker,

    Hope this helps,

    /Dennis

  • David Hyslop 27 posts 181 karma points
    Jan 05, 2016 @ 13:18
    David Hyslop
    0

    Thanks Dennis

    I will give this a go.

  • David Hyslop 27 posts 181 karma points
    Jan 05, 2016 @ 13:50
    David Hyslop
    0

    Hello Dennis

    This nearly works.

    The image works fine if I add it to the pages but with no image added it isn't inheriting.

    It gives this error.

    Server Error in '/' Application. The call is ambiguous between the following methods or properties: 'Umbraco.Web.UmbracoHelper.Media(params int[])' and 'Umbraco.Web.UmbracoHelper.Media(params string[])' Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The call is ambiguous between the following methods or properties: 'Umbraco.Web.UmbracoHelper.Media(params int[])' and 'Umbraco.Web.UmbracoHelper.Media(params string[])'

    Source Error:

    Line 433:@{
    Line 434: if (CurrentPage.HasValue("mainImage", true)){
    Line 435: var dynamicMediaItem = Umbraco.Media(CurrentPage.mainImage); Line 436: @dynamicMediaItem.Name Line 437: }

    Any ideas

    thanks.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jan 05, 2016 @ 19:30
    Dennis Aaen
    101

    Hi David,

    I made a little mistake. The code below should do what you are after.

    @{
        if(Model.Content.HasValue("mainImage", true)){
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("mainImage",true)); 
            <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")"/>    
        }   
    }   
    

    So if the current page have attached an image / logo then it will use this one for the page, If not, then it will go up in the content tree until it finds a page with the field "mainImage" and this field has a value.

    Hope this helps, and make sense.

    /Dennis

  • David Hyslop 27 posts 181 karma points
    Jan 06, 2016 @ 10:12
    David Hyslop
    0

    Thanks Dennis

    Works perfect, appreciate your help and quick response.

  • Yoni 49 posts 118 karma points
    Nov 06, 2016 @ 16:20
    Yoni
    0

    How can I do this in VB.Net?

  • Geoffry Brown 8 posts 79 karma points
    Nov 06, 2016 @ 19:13
    Geoffry Brown
    0

    Been awhile since I have done vb.net but this looks like it should be correct.

    @If Model.Content.HasValue("mainImage", true) Then
        Dim mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("mainImage",true))
        <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")"/> 
    End If
    
Please Sign in or register to post replies

Write your reply to:

Draft