Support

Account

Home Forums ACF PRO Using Parameters inside Shortcodes

Solved

Using Parameters inside Shortcodes

  • 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' );
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.