Support » Plugin: Code Snippets » Add variable to shortcode

  • Resolved ukrainaevents

    (@ukrainaevents)


    Hey, help me please

    I use code:

    <?php
    
    $category = get_category($idd);
    $count = $category->count;
     //echo $count;
    if( $count > $something ) {
    
       echo '<span class = "rv1">' . $count . ' reviews</span>' ;
    } 
    
     
    
    ?>

    and shortcode:

    [code_snippet id=6 php=true idd=11]

    but idd=11 – not passed to function….. How I can do this?

    Thanks

    • This topic was modified 11 months, 4 weeks ago by ukrainaevents.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter ukrainaevents

    (@ukrainaevents)

    anybodey?

    An answer to this would interest me as well

    Plugin Author Shea Bunge

    (@bungeshea)

    Hi @ukrainaevents and @tawarien,

    Unfortunately, we do not currently support passing though custom arbitrary attributes through to the shortcode function.

    It’s definitely something we could add in the future – if you’re able to create a new feature request on our roadmap site (https://roadmap.codesnippets.pro/) then I’ll definitely look into it for an upcoming version.

    In the meantime, what you are looking to achieve might be more easily accomplished by writing a Function snippet that calls add_shortcode directly.

    Thread Starter ukrainaevents

    (@ukrainaevents)

    Function snippet that calls add_shortcode directly.

    —-

    can y pls show example?

    Plugin Author Shea Bunge

    (@bungeshea)

    Here is an example:

    // Change this into something more appropriate.
    $shortcode_tag = 'my_shortcode';
    
    add_shortcode( $shortcode_tag, function ( $atts ) use ( $shortcode_tag ) {
    	$atts = shortcode_atts( [
    		'idd' => '',
    	], $atts, $shortcode_tag );
    
    	$category = get_category( $atts['idd'] );
    	$count = $category->count;
    
    	if ( $count > 1 ) {
    		return sprintf( '<span class="rv1">%s reviews</span>', $count );
    	}
    
    	return '';
    } );
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add variable to shortcode’ is closed to new replies.