0

    <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">

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

4

1 回答 1

0

就像@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 控制并且具有页面类型,我建议您考虑将 aPostRegion或添加PostModelRegion到该页面。这些区域会自动将一组帖子加载到页面模型中,您可以指定数量、排序顺序和其他一些内容。这将简化您重用页面类型的过程,例如更改为不同页面实例显示的帖子类型。

问候

哈坎

于 2016-03-15T10:30:31.707 回答