【问题标题】:Random order in WordPress Loop?WordPress循环中的随机顺序?
【发布时间】:2014-04-19 04:39:00
【问题描述】:

我正在使用分类档案(艺术家)中的基本循环代码,我想知道如何设置循环以随机顺序显示帖子('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 wordpress loops


    【解决方案1】:

    试试这个:

    <?php
    
    $args = array(
        'orderby' => 'rand'
    );
    $query = new WP_Query($args);
    
    if (have_posts()) {     
    
        while ( $query->have_posts() ) : $query->the_post();
    
            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;
    

    ?>

    【讨论】:

      【解决方案2】:

      query_posts(数组( 'showposts' => 6, 'orderby' => 'rand', 'category_name' => 'News' //可以插入任何类别名称 ));

      【讨论】:

        【解决方案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;
                ?>
        

        more info for query

        【讨论】:

        • 在处理自定义wP_Query 对象时,您应该使用while ( $query-&gt;have_posts() ) : $query-&gt;the_post();
        • 简单干净的解决方案!请注意,循环可能存在于不同的文件中(例如,index.php、home.php、template-centered.php 等)
        • 完美。谢谢!
        【解决方案4】:

        您有两种方法。第一种方式不是最好的方式,但你可能更容易理解:

        使用 WP_Query

        <?php
        
        $args = array(
            'orderby' => 'random'
            );
        
        $query = new WP_Query( $args );
        
        if( $query->have_posts() ):
            // Start the Loop.
            while ( $query->have_posts() ) : $query->the_post();
        
                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;
        

        在这里,我们将使用自定义 WP_Query 对象和 orderby 来获取随机帖子。


        使用 pre_get_posts

        最好的方法是使用pre_get_post 操作自动修改页面输出。不过,您可能需要更多编码。

        【讨论】:

        • 如果您希望每个类别中的所有帖子都随机化,那么您可以使用向 functions.php 添加 pre_get_post 函数,如下所示: add_action( 'pre_get_posts', 'randomise' );函数 randomise($query) { $query->set('orderby', 'rand'); }
        【解决方案5】:

        先问好问题!

        您可以通过 PHP 的简单使用功能来做到这一点。 http://www.php.net/manual/en/function.shuffle.php

        按照以下步骤:

        1. 首先是通过查询获取所有帖子
        2. 您知道 wordpress 将以数组格式提供结果。所以,不要尝试更多的过于复杂的编码。
        3. 现在您有了结果数组,所以只需使用 PHP 函数 shuffle。 http://www.php.net/manual/en/function.shuffle.php

        如果有任何疑问,请在实施后询问我。

        谢谢!

        【讨论】:

        • 这不适用于 OP 的要求。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多