L
L
linklee2018-05-31 18:34:08
WordPress
linklee, 2018-05-31 18:34:08

Display products in wordpress in random order?

I don’t understand php at all, (however, there is a python + rails background)
We need to fix a small point on the site, namely: now the last 20 items are displayed on the category page.
We need to make them be from the same category, but in random order (change sorting)
This is how it looks now: (code is not mine)

<?php echo category_description( get_category_by_slug('category-slug')->term_id ); ?></div>
<?php $col = 1;  $counter = 0;?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php ++$counter;?>
         .......

I clicked on have_posts() - go to definition and ide highlighted 2 files for me:
wp-includes/class-wp.query.php
* @return bool True if posts are available, false if end of loop.
     */
    public function have_posts() {
        if ( $this->current_post + 1 < $this->post_count ) {
            return true;
        } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
            /**
             * Fires once the loop has ended.
             *
             * @since 2.0.0
             *
             * @param WP_Query $this The WP_Query instance (passed by reference).
             */
            do_action_ref_array( 'loop_end', array( &$this ) );
            // Do some cleaning up after the loop
            $this->rewind_posts();
        } elseif ( 0 === $this->post_count ) {
            /**
             * Fires if no results are found in a post query.
             *
             * @since 4.9.0
             *
             * @param WP_Query $this The WP_Query instance.
             */
            do_action( 'loop_no_results', $this );
        }

        $this->in_the_loop = false;
        return false;
    }

and query.php file
* @since 1.5.0
 *
 * @global WP_Query $wp_query Global WP_Query instance.
 *
 * @return bool
 */
function have_posts() {
    global $wp_query;
    return $wp_query->have_posts();
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question