wordpress 对已排序的随机数据进行升序排序

您所在的位置:网站首页 提款机键盘数字排序 wordpress 对已排序的随机数据进行升序排序

wordpress 对已排序的随机数据进行升序排序

#wordpress 对已排序的随机数据进行升序排序| 来源: 网络整理| 查看: 265

我的要求 例如 - 我有 20 个帖子 (wordpress) - 我需要按升序排列 10 个帖子(按自定义元键排序) - 然后我需要显示已排序数据中的随机 1 个帖子(来自 10 个帖子)

我的代码在 function.php

add_filter( 'the_posts', function( $posts, WP_Query $query ) { if( $pick = $query->get( '_shuffle_and_pick' ) ) { shuffle( $posts ); $posts = array_slice( $posts, 0, (int) $pick ); } return $posts; }, 10, 2 );

在模板页面

$args =array('post_type' => 'products', 'posts_per_page' => 10, 'meta_key' => '_showoption_', 'orderby' => 'meta_value', 'order' => 'asc', 'no_found_rows' => 'true', '_shuffle_and_pick' => 1 , ); $my_query = new WP_Query( $args );

我的结果:从 10 个升序帖子中获得 1 个帖子:但我需要从这 10 个帖子中随机选择 1 个



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3