Copied to clipboard

Flag this post as spam?

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


  • Dem Muk 5 posts 106 karma points
    Mar 14, 2017 @ 14:14
    Dem Muk
    0

    Displaying image from different content type

    I would like to display an image from content type "A" inside content type "B". This involves checking if content type "A" does not have an image before displaying image from content type "B". Can anyone help please?

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Mar 14, 2017 @ 18:35
    Kevin Jump
    100

    Hi

    it does depend a bit on how you are finding Content Type 'A" in your code. but as an example - if we assume you have some form of content picker on your content node that lets the user pick content type 'a' from b:

    // get the content ID from the current page (Content "B")
    var contentId = Model.GetPropertyValue<int>("contentPicker");
    
    // get the content Node
    var contentNode = Umbraco.TypedContent(contentId);
    if (contentNode != null)
    {
        // check the property is there and has a value
        if (contentNode.HasProperty("image") && contentNode.HasValue("image"))
        {
            var imgId = contentNode.GetPropertyValue<int>("image");
            var img = Umbraco.TypedMedia(imgId);
            if (img != null)
            {
                // you have the image here...
                <img src="@img.Url" />
            }
        }
    }
    

    assuming :

    • "contentPicker" is a property on the content item where the user picks the content with the image
    • "image" is the property with the image in it.
Please Sign in or register to post replies

Write your reply to:

Draft