Shuffle in WordPress Loop?

I use the basic loop code in the taxonomy archive (artists), and I was wondering how you can set the loop to display messages in random order ('orderby' => 'rand'), it doesn't seem to work when I add an array? Any help would be great!

        <?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;
        ?>
+4
source share
5 answers
 <?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;
        ?>

additional information for request

+4
source

query_posts (array ('showposts' => 6, 'orderby' => 'rand', 'category_name' => 'News' // you can insert any category name));

0
source

:

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

? >

-1

. - , :

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

The best way to do this is to use an pre_get_postaction to automatically change the page output. You may need some more coding.

-2
source

Source: https://habr.com/ru/post/1531699/


All Articles