本文介绍了Silverstripe-博客帖子订购的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本来应该很容易,但是我似乎为此感到挣扎.

Silverstripe博客如何对帖子进行排序?我想将特定的博客文章固定在列表的顶部,因此我创建了一个SortOrder字段,并将其值设置为1.尝试按先后按SortOrder和PublishDate进行排序,但似乎始终只能按PublishDate进行排序./p>

即使在博客模型上更改此设置也无济于事:

private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC' ;
解决方案

更新BlogPostdefault_sort应该有效:

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

扩展BlogPost以添加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
        );
    }

}
 

确保您要粘贴的BlogPostSticky设置为true的情况下发布.

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' ;
解决方案

Updating the default_sort of BlogPostshould 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.

这篇关于Silverstripe-博客帖子订购的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:36