0

I would like to create a shortcode that has predefined output for attribute values. For example:

[shortcode att="value1"]

output = <div id="value1">Predefined content for value1</div>

[shortcode att="value2"]

output = <span id="value2">Predefined content for value2</span>

etc.

I'm just not sure how to setup the function with a predefined array of output values. Ideally the function would look something like this:

array(
   value1 = '<div id="value1">',
   value1 = '<div id="value2">'
);

I hope this makes sense. Thanks!

0

According to Codex:

    
function shortcode_function_name( $atts ) {
   $atts = shortcode_atts( array(
      'attr1' => '',
   ), $atts, 'your-shortcode-name' );

   return '<div id="'.$atts['attr1'].'">Your Predefined Content</div>';
}
add_shortcode( 'your-shortcode-name', 'shortcode_function_name' );
    

Adding [your-shortcode-name attr1="Value1"] to the content of any post/page will give the following output <div id="Value1">Your Predefined Content</div>.

Adding [your-shortcode-name attr1="Value1"] to the content of any post/page will give the following output <div id="Value1">Your Predefined Content</div>.

etc...

And you can deep by adding the predefined content inside the shortcode, this way you can always change the predefined content inside the shortcode as well.

Hope it helps!

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.