Sticky posts in Essential Grid

Hi,

I managed to get my sticky posts appear on top in my ess-grid using the following code snippet from Themepunch in Functions.php:

=======================================

add_filter(‘essgrid_modify_posts’, ‘eg_modify_post’, 10, 2);

function eg_modify_post($posts, $grid_id){

/* 
    Your grid's ID can be found where your grid's are 
    listed in the plugin's main admin page.
*/

$your_grid_id = 84;

/* *************************************** */
/* no need to modify any of the below code */
/* *************************************** */

if($grid_id != $your_grid_id) return $posts;

$stickys = array();
$regular = array();

foreach($posts as $post) {

    if(!is_sticky($post['ID'])) $regular[] = $post;
    else $stickys[] = $post;

}

return array_merge($stickys, $regular);

}

==========================================================

As you can see this code applies to grid ID 84.

My question is:
Can I make this code apply to more ID’s?
I tried putting the whole code in twice with two different ID’s but that doesn’t work.
I also tried to sum up two ID’s like this:

$your_grid_id = 84, 85;

And I also tried:
$your_grid_id = 84;
$your_grid_id = 85;

But that does not work either.

Your help is much appreciated!

Regards, Henk

1 Like

Hello Henk,

1.) To achieve that, let’s declare $your_grid_id an array like this:
$your_grid_ids = array(84,85,86);

2.) Then change this line:
if($grid_id != $your_grid_id)
To this:
if (in_array($grid_id, $your_grid_ids, true))

Check this guide: https://php.net/manual/en/function.in-array.php

Hope this helps.

1 Like

Hi Lely,

Great! This works like a charm.
Thanks also for the link.

Have a nice weekend!

Regards, Henk

You’re most welcome Henk! :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.