2016-03-14 28 views
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"> 

它不會出現工作如何我有點什麼,我需要做的任何建議 ?

+0

@ post.Attachments表示的陣列,而不是單個圖像。是這樣嗎? – andreasnico

+0

並非他們都存儲在CMS中,並且與帖子沒有直接關係。 所以我希望引用附件中的圖像,使它更具動感。 – TuckRollworthy

回答

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控制並具有網頁型我建議你考慮添加無論是PostRegionPostModelRegion到該頁面。這些區域自動將一系列帖子加載到頁面模型中,您可以指定數量,排序順序&其他一些東西。這將簡化您重用頁面類型的操作,但例如更改要爲不同頁面實例顯示的帖子類型。

問候

哈坎

+0

你是個傳奇人物! – TuckRollworthy

相關問題