加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – WordPress循环中的随机顺序?

发布时间:2020-12-13 22:51:58 所属栏目:PHP教程 来源:网络整理
导读:我在分类档案(艺术家)中使用基本循环代码,我想知道如何设置循环以随机顺序显示帖子(‘orderby’=’rand’)它似乎不起作用我添加数组?任何帮助都会很棒! ?php // Start the Loop. while ( have_posts() ) : the_post(); /* * Include the post format-speci
我在分类档案(艺术家)中使用基本循环代码,我想知道如何设置循环以随机顺序显示帖子(‘orderby’=>’rand’)它似乎不起作用我添加数组?任何帮助都会很棒!

<?php
                // Start the Loop.
                while ( have_posts() ) : the_post();

                    /*
                     * Include the post format-specific template for the content. If you want to
                     * use this in a child theme,then include a file called called content-___.php
                     * (where ___ is the post format) and that will be used instead.
                     */
                    array ( 'orderby' => 'RAND' );
                    get_template_part( 'content',get_post_format() );

                endwhile;
                // Previous/next page navigation.
                twentyfourteen_paging_nav();

            else :
                // If no content,include the "No posts found" template.
                get_template_part( 'content','none' );

            endif;
        ?>

解决方法

<?php  

$query = new WP_Query( array ( 'orderby' => 'rand','posts_per_page' => '-1' ) );

        if( $query->have_posts() ):
                // Start the Loop.
                while ( $query->have_posts() ) : $query->the_post();

                    /*
                     * Include the post format-specific template for the content. If you want to
                     * use this in a child theme,then include a file called called content-___.php
                     * (where ___ is the post format) and that will be used instead.
                     */

                    get_template_part( 'content','none' );

            endif;
        ?>

more info for query

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读