• Resolved denisecontent

    (@denisecontent)


    Hi I am a long time user of Customizr theme – absolutely love it.

    One thing that has forever stumped me – how can modify category pages to display posts in a random order across pages. When I use pre_get_posts (which is what everywhere on the web says to use), I am still getting duplicate displays on subsequent category pages.

    Right now I am trying out a modified version of pre_get_posts that is supposed to work on many themes but not for me!

    Any ideas on how to make this work?

    Here is my demo page I am playing with – hhttps://www.calaway.it/demo4/category/Alabama/

    HELP!

    Denise

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter denisecontent

    (@denisecontent)

    Oh just in case anyone asks – I have caching on my server turned off – so that should NOT be an issue. And I have been clearing my browser just to make sure.

    Thread Starter denisecontent

    (@denisecontent)

    I figured it out.

    First put this code into your Child theme’s function.php file:

    
    session_start();
    
    add_filter( 'posts_orderby', 'randomise_with_pagination' );
    function randomise_with_pagination( $orderby ) {
    	if( is_category( 'Alabama' )  ) {
    	  	// Reset seed on load of initial archive page
    		if( ! get_query_var( 'paged' ) || get_query_var( 'paged' ) == 0 || get_query_var( 'paged' ) == 1 ) {
    			if( isset( $_SESSION['seed'] ) ) {
    				unset( $_SESSION['seed'] );
    			}
    		}
    	
    		// Get seed from session variable if it exists
    		$seed = false;
    		if( isset( $_SESSION['seed'] ) ) {
    			$seed = $_SESSION['seed'];
    		}
    	
    	    	// Set new seed if none exists
    	    	if ( ! $seed ) {
    	      		$seed = rand();
    	      		$_SESSION['seed'] = $seed;
    	    	}
    	
    	    	// Update ORDER BY clause to use seed
    	    	$orderby = 'RAND(' . $seed . ')';
    	}
    	return $orderby;
    }

    Now next –
    You need to create a php file for each category using these instructions: https://docs.presscustomizr.com/article/172-using-the-wordpress-template-hierarchy-with-the-customizr-theme.

    Then place your php randomizing code within the php file – which is:

    <?php /* Start the Loop */ ?>
    
    <?php
    if (is_category(‘your-category-slug-name’)) {
    query_posts($query_string . ‘&orderby=rand’);
    }
     else {
    query_posts($query_string . ‘&orderby=title&order=ASC’);
    }
    ?>
    <?php while ( have_posts() ) : the_post(); ?>

    Now Customizr’s instructions are to add this file to the Child Theme. I could not make this work for my site. So I tried going to the root level of the template where index.php is and I added it there.

    Once I did all that – I refreshed the category page and wa la – my category posts were now displaying in random order across all pages without duplicates.

    Thread Starter denisecontent

    (@denisecontent)

    How do I add categories?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Random post order for category pages’ is closed to new replies.