问题
Thought this would be fairly easy but I seem to be struggling with this.
How does the Silverstripe blog sort its posts? I want to pin a specific blog post to the top of the list so I created a SortOrder field and gave it a value of 1. Tried to sort by SortOrder and then by PublishDate but it only seems to sort by PublishDate all the time.
Even changing this on the blog model doesn't do anything:
private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC' ;
回答1:
Updating the default_sort
of BlogPost
should work:
# In your config.yml
BlogPost:
default_sort: 'Sticky DESC, PublishDate DESC'
extensions:
- MyBlogPostExtension
Extend BlogPost to add a Sticky
boolean (this could also be an 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
);
}
}
Make sure that the BlogPost
you want stickied is published with Sticky
set to true.
回答2:
I struggled with blog, sort & lumberjack (Post in GridField not Sitetree) a bit. I use heyday/silverstripe-gridfieldversionedorderablerows to have it manually sortable.
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"
));
}
}
I made my own PaginatedListSorted on a DataExtension of Blog but you probable just could set sort as Janne Klouman suggested per yml.
来源:https://stackoverflow.com/questions/43247905/silverstripe-blog-post-ordering