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 6 years, 2 months ago by
Deon.
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 );