I successfully use the following code below to call a shortcode that looks like this:
[show_valuesofstuff]
But I want to include a parameter for the department value so that I can use the shortcode dynamically like:
[show_valuesofstuff department=”Legal Studies”]
You can see below that I have hardcoded in the value of Criminal Justice and I’m hoping that I can make this dynamic.
<?php
add_shortcode('show_valuesofstuff', function() {
?>
<?php
$args1 = array(
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'exclude' => array(1,8,9),
'meta_query' => array(
'relation' => 'AND',
'department' => array(
'key' => 'department',
'value' => '"Criminal Justice"', // I WANT THIS VARIABLE IN THE SHORTCODE
'compare' => 'LIKE',
),
)
);
$subscribers = get_users($args1);
echo '<ul>';
foreach ($subscribers as $user) {
echo '<li>' . $user->display_name.' ['.$user->phone_number . ']</li>';
}
echo '</ul>';
?>
<?php
}); // END SHORTCODE [show_valuesofstuff]
?>
Can you please help me form the extra code to make this possible?
Thanks!
I reworked my code to look like this and everything is working so far!
<?php
/**
* Shortcode: [deptlist department=""]
* Description: This is how stuff works with parameters
*/
function dept_option($atts){
extract(shortcode_atts( array(
'department' => 'Legal Studies',
), $atts ));
$myfinalcode = '"' . $department . '"';
$args1 = array(
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'exclude' => array(1,8,9),
'meta_query' => array(
'relation' => 'AND',
'department' => array(
'key' => 'department',
'value' => $myfinalcode,
'compare' => 'LIKE',
),
)
);
$subscribers = get_users($args1);
echo '<ul>';
foreach ($subscribers as $user) {
echo '<li>' . $user->display_name.' ['.$user->phone_number . ']</li>';
}
echo '</ul>';
// ENDING ROW
}
add_shortcode( 'deptlist', 'dept_option' );
?>
The topic ‘Using Parameters inside Shortcodes’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.