• 让不同类型页面文章展示顺序各异
  • 让不同类型页面文章展示顺序各异-WP新手学园

    如果你在WordPress中使用标准循环,即用到了wp_query进行查询,你可以在参数中自定义排序类型,默认情况下是按照时间排序的,但是有些时候,你需要根据不同情况让不同页面,比如最近更新或者文章归档或者更多的作品展示页面按照自定义的方式进行排序,那么可以通过下面的代码来解决问题:

    上面的代码实质是在修改了循环的排序方式,从代码中不难看出,其中的orderby就是排序方式,那么无论你怎么修改,只要是利用的标准循环,那么orderby的选项始终只有下面几个:

  • ‘none‘ – No order (available since version 2.8).
  • ‘ID‘ – Order by post id. Note the capitalization.
  • ‘author‘ – Order by author.
  • ‘title‘ – Order by title.
  • ‘name‘ – Order by post name (post slug).
  • ‘type‘ – Order by post type (available since version 4.0).
  • ‘date‘ – Order by date.
  • ‘modified‘ – Order by last modified date.
  • ‘parent‘ – Order by post/page parent id.
  • ‘rand‘ – Random order.
  • ‘comment_count‘ – Order by number of comments (available since version 2.9).
  • ‘relevance‘ – Order by search terms in the following order: First, whether the entire sentence is matched. Second, if all the search terms are within the titles. Third, if any of the search terms appear in the titles. And, fourth, if the full sentence appears in the contents.
  • ‘menu_order‘ – Order by Page Order. Used most often for pages (Order field in the Edit Page Attributes box) and for attachments (the integer fields in the Insert / Upload Media Gallery dialog), but could be used for any post type with distinct ‘menu_order‘ values (they all default to 0).
  • ‘meta_value‘ – Note that a ‘meta_key=keyname‘ must also be present in the query. Note also that the sorting will be alphabetical which is fine for strings (i.e. words), but can be unexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as you might naturally expect). Use ‘meta_value_num‘ instead for numeric values. You may also specify ‘meta_type‘ if you want to cast the meta value as a specific type. Possible values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’, same as in ‘$meta_query‘. When using ‘meta_type’ you can also use ‘meta_value_*’ accordingly. For example, when using DATETIME as ‘meta_type’ you can use ‘meta_value_datetime’ to define order structure.
  • ‘meta_value_num‘ – Order by numeric meta value (available since version 2.8). Also note that a ‘meta_key=keyname‘ must also be present in the query. This value allows for numerical sorting as noted above in ‘meta_value‘.
  • ‘post__in‘ – Preserve post ID order given in the post__in array (available since version 3.5). Note – the value of the order parameter does not change the resulting sort order.
  • ‘post_name__in‘ – Preserve post slug order given in the ‘post_name__in’ array (available since Version 4.6). Note – the value of the order parameter does not change the resulting sort order.
  • ‘post_parent__in‘ -Preserve post parent order given in the ‘post_parent__in’ array (available since Version 4.6). Note – the value of the order parameter does not change the resulting sort order.
  • 上面的内容来自官方,应该算是比较权威的了,如果感兴趣的话,可以去这个链接看看相关介绍: https://developer.wordpress.org/reference/classes/wp_query/

    不过换句话说,学会举一反三,这段代码虽然是作用与排序,那么言外之意,我们可以将里面的代码替换为wp_query里面的任何一个参数,比如将orderby替换为cat,即分类。那么可以实现的就是根据不同情况下展示不同分类的样式亦或者内容等等,这些就需要大家去发挥了。

  • pre_get_posts 函数介绍
  • 函数用法
  • do_action_ref_array( 'pre_get_posts', WP_Query $query )

    Fires after the query variable object is created, but before the actual query is run.

  • 功能介绍
  • Note: If using conditional tags, use the method versions within the passed instance (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions like is_main_query() test against the global $wp_query instance, not the passed one.

  • 参数介绍
  • $query

    (WP_Query)
    The WP_Query instance (passed by reference).

  • 返回值
  • 暂无数据
  • 订阅站点了解最新技巧
  • 任何有用的技巧我们均将通过邮件告知你
  • 点击订阅
    高级设置
  • 0
    大惊
    0
    大笑
    0
    发呆
    0
    恶魔
    0
    流泪
    0
    调皮
    0
    笑出泪
    0
    犯困
    0
    坏笑
    0
    疑问
  • 版权声明:整理不易,如无特殊说明,文章内容均为本站原创,转载请注明出处
  • 本文标题:让不同类型页面文章展示顺序各异
  • 本文链接:https://www.wpxsxy.com/127.html [点击复制]
  • W*******
  • 2830776172
  • qweqw
  • qwe
  • 啥都没有,没啥好看的
    主页
    关注
    聊天
    关于wp_is_mobile()函数的讨论
  • 上一篇:
  • 关于wp_is_mobile()函数的讨论
  • 注册成功后更新用户数据
  • 下一篇:
  • 注册成功后更新用户数据
  • 相关推荐
    文章评论
    提交评论
    分类推荐