Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add helper plugins list_posts,get_posts #467

Merged
merged 3 commits into from
Feb 5, 2014
Merged

add helper plugins list_posts,get_posts #467

merged 3 commits into from
Feb 5, 2014

Conversation

chenall
Copy link
Contributor

@chenall chenall commented Jan 25, 2014

使用这两个可以很方便的获取符合条件的文章.

默认参数如下:

{
    query:{tags: '',categories: '',operator: ''},
    count: 6,  //要获取的文章数量
    orderby: 'date', //排序依据, 'random' ,代表随机排序.
    order: -1, //正反排序, -1 反向,,其它正向.
    ulClass: '', 
    liClass: '',
    style: 'list',
    separator: ', ',
    class: 'post',//ulClass和liClass如果省略时该参数有效,ulClass = class,liClass= class + '-list-item';
}

query参数介绍

加上query参数可以根据指定条件查询符合条件的文章.

若设置了tags参数,只要文章的标签有其中之一该文章就会被列出来.
categories 参数和tags参数一样.根据分类查询.

tagscategories 可以使用字符串(多个使用','分隔)或数组,还可以直接从post中获取,比如post.tags

注: 为了使用方便,有经过特殊处理,标签和分类查找是不区分大小写的.所以grub4dosGRUB4DOS是一样的

operator 是操作符号,指定查询条件的关系,默认是'and',每一个查询条件都要符合.如果是'or',就是只要有一个条件符合就行了.
operator 可使用的值为 or and not nor;

你还可以添加其它查询,比如,以下查找id是8的文章(当然了你需要在文章中设置id的值.)

query{id:8}

查询参数还可以是一个自定义函数,function(data){return xxxxxx}
这个函数可以自己写,只有一个参数,

以下例子就可以列出包含字符串"通用"的文章

list_posts(query:{title: function(data){return //.test(data)}});

通过自定义函数,你可以实现功能更强大的查询;

简单应用例子

  1. 获取最近更新的8个贴子列表

    list_posts({count:8,orderby: "updated",order:-1}
  2. 获取最近更新的并且有带标签'GRUB4DOS'的贴子

    list_posts({count:8,orderby: "updated",order:-1,query:{tags: 'GRUB4DOS'}}
  3. 在主题中使用,可以根据文章的标签和分类查找相关贴子 Related posts #437
    operatoror ,所以只要分类或标签有在列表中就行.

      list_posts({query{operator:'or', tags: item.tags,categories: item.categories}})
  4. 获取随机贴子列表

    list_posts({orderby: 'random'})

注: 参数可以组合使用,这样很方便提取特定的文章..

以上是list_posts的功能,主要是返回一个网页可显示的列表.

如果你还需要自己处理,这时可以使用get_posts

如下,可以获取到包含标签grub4dos的所有文章.

var posts = get_posts({query:{tags: 'GRUB4DOS'}});
posts.forEach(function(post){
...
})

return posts.find(query,query_opt);
}

return options.count?posts.limit(options.count):posts;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虚到options.count参数有可能人为置空的情况.像{count:''}

@chenall
Copy link
Contributor Author

chenall commented Jan 26, 2014

OK,改好了.統一風格的代碼看起來會比較舒服.

tommy351 added a commit that referenced this pull request Feb 5, 2014
add helper plugins `list_posts`,`get_posts`
@tommy351 tommy351 merged commit f766c86 into hexojs:master Feb 5, 2014
@chenall chenall deleted the add_list_posts_helper branch July 10, 2014 05:40
@revelt
Copy link

revelt commented Aug 19, 2014

Guys, is there are way to skip certain number of articles in the query? I need articles 5 to 8 for example.

@chenall
Copy link
Contributor Author

chenall commented Sep 17, 2014

var posts = get_posts({query:{tags: 'GRUB4DOS'}});
posts = posts.slice(5,8);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants