Ibrarr Khan
Ibrarr Khan

Reputation: 55

How to run an if statement against a shortcode attribute value in wordpress

I want to run an if statement on an attribute in my shortcode but for some reason it wont work?

I know that the attribute is being passed through because when I echo it were 'working' currently is I see the value so not sure what's wrong with my if statement?

if ( $atts['type'] == 'Platinum' ) {
?>
    <h2><?php echo 'working'?></h2>
<?php
} else {}

Upvotes: 0

Views: 745

Answers (1)

Ayus Mohanty
Ayus Mohanty

Reputation: 390

You can use shortcode attributes like below.

<?php
function function_name($atts) {
  extract(shortcode_atts(array(
    'var1' => 'value1', //Assigning default value
    'var2' => 'value2',
  ), $atts));
  if($var1 == 'Platinum'){
    //Do things
  } else {
    //Do other things
  }
}//Function ends
add_shortcode('shortcode_name', 'function_name');

Upvotes: 1

Related Questions