Created
November 26, 2021 05:23
-
-
Save reachkamrul/09db08a5bcbfbd3f5e8a90c382ab20c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* The following functions will add additional shortcode | |
* In this case, you can use a shortcode to show a random data among your table cell | |
* You can use like this [ninja_table_random_cell id="Your Table ID" column="Your Colum Key"] | |
* Just add this snippet to your theme's functions.php file or relevant place. | |
*/ | |
add_shortcode('ninja_table_random_cell', function ($atts) { | |
$shortCodeDefaults = array( | |
'id' => 0, | |
'column' => '' | |
); | |
$shortCodeData = shortcode_atts($shortCodeDefaults, $atts); | |
extract($shortCodeData); | |
if (!$id || !$column) { | |
return ''; | |
} | |
$id = absint($id); | |
$total = ninjaDB()->table('ninja_table_items') | |
->where('table_id', $id) | |
->count(); | |
if(!$total) { | |
return ''; | |
} | |
$random = rand(1,$total); | |
$offset = $random -1; | |
$row = ninjaDB()->table('ninja_table_items') | |
->where('table_id', $id) | |
->offset($offset) | |
->first(); | |
$values = json_decode($row->value, true); | |
$text = \NinjaTables\Classes\ArrayHelper::get($values, $column, ''); | |
return '<blockquote class="nt_qoute">'.$text.'</blockquote>'; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment