2017-04-06 140 views
1

認爲這將是相當容易的,但我似乎正在努力與此。Silverstripe - 博客文章訂購

Silverstripe博客如何對其帖子進行排序?我想將一個特定的博客文章固定到列表的頂部,所以我創建了一個SortOrder字段並賦予其值爲1.試圖按SortOrder和PublishDate排序,但它似乎一直按PublishDate排序。

即使改變這個博客上的模型沒有做任何事情:

private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC' ; 
+0

更新default_sort應該在你的config.yml中工作,例如'BlogPost:default_sort:'SortOrder DESC,PublishDate DESC''。然後,將'SortOrder'設置爲1爲您的粘貼文章應該放在頂部,假設其他BlogPosts具有較低的值。確保'BlogPost_Live.SortOrder'設置爲1 - 適用於我。 –

回答

5

更新的BlogPostdefault_sort應該工作:

# In your config.yml 
BlogPost: 
    default_sort: 'Sticky DESC, PublishDate DESC' 
    extensions: 
    - MyBlogPostExtension 

擴展博文添加Sticky布爾(這也可能是Int):

class MyBlogPostExtension extends DataExtension 
{ 

    private static $db = [ 
     'Sticky' => 'Boolean' 
    ]; 

    public function updateCMSFields(FieldList $fields) 
    { 
     $stickyField = CheckboxField::create(
      'Sticky', 
      'Sticky this blogpost' 
     ); 

     $fields->addFieldToTab(
      'Root.Main', 
      $stickyField 
     ); 
    } 

} 

確保要粘貼的BlogPost已發佈,並且Sticky設置爲true。

+0

這是完美的!謝謝 – Craig

1

我在博客上苦苦掙扎,排序& lumberjack(在GridField中的帖子不是Sitetree)。我使用heyday/silverstripe-gridfieldversionedorderablerows手動排序。

Injector: 
    GridFieldConfig_BlogPost: 
    class: GridFieldConfig_MyBlogPost 

<?php 
class GridFieldConfig_MyBlogPost extends GridFieldConfig_BlogPost 
{ 
    public function __construct($itemsPerPage = null) 
    { 
     parent::__construct($itemsPerPage); 
     $this->addComponent(new GridFieldVersionedOrderableRows('Sort')); 
     $this->getComponentByType("GridFieldPaginator")->setItemsPerPage(100); 
     $this->getComponentByType("GridFieldDataColumns")->setDisplayFields(array(
      "BlogThumbnail" => "Thumbnail", 
      "Title" => "Title" 
     )); 
    } 
} 

我做我自己的PaginatedListSorted上博客的DataExtension但你可能只是可以設置排序爲詹恩Klouman每陽明建議。