提问者:小点点

WordPress循环中的随机顺序?


我在分类学归档(艺术家)中使用基本的循环代码,我想知道如何设置循环以随机顺序显示帖子('orderby'=

        <?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;
        ?>

共3个答案

匿名用户

 <?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', 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;
        ?>

查询更多信息

匿名用户

query_posts数组

匿名用户

先问个好问题!

您可以使用PHP的简单函数来实现这一点。http://www.php.net/manual/en/function.shuffle.php

遵循以下步骤:

  1. 第一件事是获取所有的帖子与查询
  2. 你知道wordpress将提供数组格式的结果。所以,不要尝试更多的编码,它太复杂了。
  3. 现在你有了结果数组,所以只需要使用PHP函数洗牌。http://www.php.net/manual/en/function.shuffle.php

如有疑问,请在实施后问我。

谢谢