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

    add_action('pre_get_posts', 'change_order');
    
    function change_order($query) {
        if ( is_front_page() || is_home() ) {
            $query->set( 'orderby', 'menu_order' );
            $query->set( 'order', 'ASC' );
        }
        return $query;
    }
    

    上面的代码实质是在修改了循环的排序方式,从代码中不难看出,其中的orderby就是排序方式,那么无论你怎么修改,只要是利用的标准循环,那么orderby的选项始终只有下面几个: [list title="" type="" unique_number="list_1c1e3f8e00814c77bfecd965ec1c7afa"]‘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.[/list]

    上面的内容来自官方,应该算是比较权威的了,如果感兴趣的话,可以去这个链接看看相关介绍: https://developer.wordpress.org/reference/classes/wp_query/

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

  • 联系我们
  • 有任何疑问欢迎联系我们
  • WP新手学园
  • 让不同类型页面文章展示顺序各异
  • 邮箱:2830776172#qq.com(#换成@)
  • 回复时间:9:00-19:00
  • 如果提问.请图文并茂,详细描述问题
  • 让不同类型页面文章展示顺序各异
  • 微信号:wpxsxy_official
  • 仅支持付费解决问题
  • *并非所有问题都会被回复
  • 让不同类型页面文章展示顺序各异
  • QQ:2830776172
  • 回复时间:9:00-19:00
  • QQ仅支持付费提问,时间有限,尽情谅解
  • 让不同类型页面文章展示顺序各异
  • QQ群:198768181
  • 有问题可以进去提问,寻求群友帮助
  • *并非所有问题都会被回复
  • 让不同类型页面文章展示顺序各异
  • 咸鱼:WP新手学园
  • 如果对于直接交易不放心,可以走闲鱼
  • *咸鱼价格不作为最终交易价格
  • 备案号:鄂ICP备2021011647号-1