【问题标题】:Displaying attached image with post how to i get it to display显示带有帖子的附加图像我如何让它显示
【发布时间】:2016-03-14 12:57:36
【问题描述】:

    <div>
                    <div class="col-md-4">
                        <h3 class="textStrong">Latest Tweets</h3>
                        <a class="twitter-timeline" href="https://twitter.com/RFUK">Tweets by RFUK </a></div>
                        
                    </div>
                    <div class="col-md-4"></div>
                
                <div class="col-md-4">
                    <h2>News Feeds</h2>
 @{   
                     var news = new List<Piranha.Entities.Post>(); 
                         using (var db = new Piranha.DataContext()) { 
                          news = db.Posts 
                          .Include(p => p.CreatedBy) 
                          .Where(p => p.Template.Name == "News Post Types") 
                          .OrderByDescending(p => p.Published) 
                          .Take(4).ToList(); 
   } 
 } 
 
 
                        @foreach (var post in news) { 
                            <div class="post"> 
                             <h2><a href="@UI.Permalink(post.PermalinkId)">@post.Title</a></h2> 
                                 <p class="meta">Published @post.Published.Value.ToString("yyyy-MM-dd") by @post.CreatedBy.Firstname</p> 
                                <p>@post.Excerpt</p> 
                                
                            <img src="@post.Attachments">    
                        
                            </div>

我在处理帖子。我有这个代码可以使用....工作得很好我可以添加..但是我希望在帖子中显示附加的图像。我该怎么做?

                            <img src="@post.Attachments">

它似乎没有任何建议 关于我如何排序我需要做的事情?

【问题讨论】:

  • @post.Attachments 表示一个数组,而不是单个图像。是这样吗?
  • 并非真的它们都存储在 CMS 中,并且与帖子没有直接关系。所以我希望参考带有附件的图像,使其更具动态性。

标签: asp.net-mvc piranha-cms


【解决方案1】:

就像@andreasnico 指出的Attachments 是引用的媒体资产ID 的集合。如果您想显示第一个附件(假设您知道它是一张图片),您可能会这样做。

@foreach (var post in news) { 
  <div class="post"> 
    <h2><a href="@UI.Permalink(post.PermalinkId)">@post.Title</a></h2> 
    <p class="meta">Published @post.Published.Value.ToString("yyyy-MM-dd") by @post.CreatedBy.Firstname</p> 
    <p>@post.Excerpt</p> 

    @if (post.Attachments.Count > 0) {
      <img src="@UI.Content(post.Attachments[0])">
    }  
  </div>
}

这将获取第一个附件的内容 URL 并将其用作图像的来源。请注意,您还可以缩放和裁剪图像以用于这样的列表:

<img src="@UI.Content(post.Attachments[0], 300, 100)">

这会将图像缩放并裁剪为 300 像素宽和 100 像素高。您可以在此处阅读更多相关信息:http://piranhacms.org/docs/api-reference/ui-helper

此外,如果显示帖子列表的页面由 CMS 控制并且具有页面类型,我建议您考虑将 PostRegionPostModelRegion 添加到该页面。这些区域会自动将一组帖子加载到页面模型中,您可以指定数量、排序顺序和其他一些内容。这将简化您重用页面类型的过程,例如更改为不同页面实例显示的帖子类型。

问候

哈坎

【讨论】:

  • 你是个传奇人物!
猜你喜欢
  • 2013-03-09
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多