In which I detail obscure WordPress tinkering and celebrate doing what happens with a small amount of understanding…
Despite the expected mockery of some harsh critics I’ve been really happy with the change over in August to this newest outer dressing on this blog (do you keep your own history? It’s a good practice if you are in this domaining for the long haul).
One of the features of the parent Cover theme is the ability to have a top sliding banner of selected “featured” posts. This is done by picking a tag you want to use, applying it to posts, and then entering it in the appropriate spot of the customizer

My site will display all posts tagged “marquee” for the top slider of the blog home.
This generally works well, but I had a thought to do something other than put selected posts in the top- I wanted to use it as a way to point to my sites that are elsewhere on the internet.
My aha was to use the Quick Page/Post Redirect Plugin to create a series of posts I could tag to put at the top of the blog, but any clicks will take you to my other sites. You can see the whole set of them at http://cogdogblog.com/tag/marquee/. For example, the “post” for my photo site http://cogdogblog.com/2017/08/barkingdog/
Actually takes you directly to http://barkingdog.me.
Each of these “posts” has a title, the marquee
tag, a featured image to make it appear on my site, and then I use the settings for Quick Page/Post Redirect Plugin to create the redirect
The thing I did not like is that the same featured post always appeared first, because they way they are retrieved, as in the tag link, is typical blog newest post first. The only way to change the order is to manually fiddle with the publishing dates.
There ought to be a better way.
A random way.
And thus the code digging begins. I found in the Cover theme template files that it makes use of the JetPack plugin feature for Featured Content.
But as far as I could tell, there are no options/parameters to send the function to change the order in which posts are returned. But… as I could see the way the code works, it requests the featured posts via a function
1 2 3 4 5 6 7 |
<?php $img = ''; $featured_posts = cover_get_featured_posts(); foreach ( (array) $featured_posts as $order => $post ) : setup_postdata( $post ); $img = cover_get_featured_image( $post->ID ); ?> |
so what this function returns is simply an array, or an associative array. Maybe I can shuffle the array once it is delivered. The thing about arrays is… there is a built in shuffle
function to shuffle ordinary arrays, but to re-order an associative array, you need a little helper function.
As typical IFTHAISA (I Found The Answer in Stack Exchange), so I wrote my own function. Basically, you get the keys for the main associative array, store in new ordinary array, then shuffle that array, then reassign the values based on the keys.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function shuffle_posts ( $array ) { // create an array based in the keys of the input $shuffleKeys = array_keys($array); // now shuffle them keys shuffle($shuffleKeys); // create a new array to store the results $newArray = array(); // now assign the values from the original array to the new one foreach( $shuffleKeys as $key ) { $newArray[$key] = $array[$key]; } // send back the shuffled array return $newArray; } |
This all goes into the /inc/jetpack.php
template of the Cover theme. The original function that gets the featured posts from JetPack is
1 2 3 4 5 6 7 8 |
/** * Getter function for Featured Content Plugin. * * @return array An array of WP_Post objects. */ function cover_get_featured_posts() { return apply_filters( 'cover_get_featured_posts', array() ); } |
which I re-write:
1 2 3 4 5 6 7 8 |
/** * Getter function for Featured Content Plugin. But we shuffle 'em * * @return array An array of WP_Post objects. */ function cover_get_featured_posts() { return shuffle_posts( apply_filters( 'cover_get_featured_posts', array() ) ); } |
The only hitch is that this needed to go into the parent theme because of the way it references the include files. For this case, an update is not a problem because the author is not updating the plugin. But if you every want to keep a theme from being auto updated you can trick it by editing the style.css
file in the parent theme and editing the Version
value in the top to a crazy high number, like changing the current theme version from 1.8.2
to
1 |
Version: 106.8.2 |
WordPress will only update a theme if the version on your site is less than the version on WordPress.
It’s a cheap trick, but it work likes a real (non-bit) gold coin.
And now in testing, I can see that my header featured images are now in different orders. The thing is… you will not see it on a reload because I am using a caching plugin. But keep returning to the front page of this blog, and you should notice it does change.
This is rather a tiny, inconsequential thing, but being able to do the tiny, inconsequential things I know should be doable, and not being limited what a system limits me to, is the most freedom a web tinkerer can ask for.
If I want random, the heck with what the code provides, I can get it myself.
Featured Image: Random Number Multiples – RGB flickr photo by blprnt_van shared under a Creative Commons (BY) license