• Resolved Deon

    (@deon-b)


    Hi,
    thank you for this amazing plugin.
    I added the shortcode to hundreds of posts and I just realized the order is always the same. I read your documentation about adding orderby="rand" but now it’s too late, I would have to manually add it in hundreds of posts, after I have already spent days adding the short code manually to them.

    Maybe there is a way to add a code in functions.php that by default displays them in random order?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Bill Erickson

    (@billerickson)

    Here’s a tutorial on customizing the default attributes: https://displayposts.com/2019/01/04/change-default-attributes/

    Thanks

    Thread Starter Deon

    (@deon-b)

    Hi,
    thanks, I did see that, and in fact I also used it on my website to set 25 as my default number of related posts.

    But which section do I have to edit to make it random by default?

    Thread Starter Deon

    (@deon-b)

    The only example you give is to change the number of posts to 20. Nowhere it’s mentioned what to change to make it random by default.

    If you find yourself typing the same parameters over and over, you can save time by changing the default attributes. These can always be overridden by specifying new attributes on an individual shortcode.

    For instance, let’s say you always list 20 posts with excerpts:

    • This reply was modified 5 years ago by Deon.
    Plugin Author Bill Erickson

    (@billerickson)

    Any attributes you type into the shortcode could be added to that function as a default attribute. The tutorial provides a general example. I didn’t write a detailed example for every one of the dozens of parameters in the shortcode.

    For your specific usage, add the following code to your theme’s functions.php file, a core functionality plugin, or the Code Snippets plugin:

    
    <?php
    
    /**
     * Set Defaults in Display Posts Shortcode
     * @see https://displayposts.com/2019/01/04/change-default-attributes/
     *
     * @param array $out, the output array of shortcode attributes (after user-defined and defaults have been combined)
     * @param array $pairs, the supported attributes and their defaults
     * @param array $atts, the user defined shortcode attributes
     * @return array $out, modified output
     */
    function be_dps_defaults( $out, $pairs, $atts ) {
    	$new_defaults = array( 
    		'orderby' => 'rand',
    	);
    	
    	foreach( $new_defaults as $name => $default ) {
    		if( array_key_exists( $name, $atts ) )
    			$out[$name] = $atts[$name];
    		else
    			$out[$name] = $default;
    	}
    	
    	return $out;
    }
    add_filter( 'shortcode_atts_display-posts', 'be_dps_defaults', 10, 3 );
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Random order by default?’ is closed to new replies.