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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – WordPress循环中的随机顺序?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我在分类档案(艺家)中使用基本循环代码,我想知道如何设置循环以随机顺序显示帖子(‘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

脚本宝典总结

以上是脚本宝典为你收集整理的php – WordPress循环中的随机顺序?全部内容,希望文章能够帮你解决php – WordPress循环中的随机顺序?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。