0

I have a problem with my shortcode that I created to run in a wordpress environment.

I'm not an expert with shortcode, that's the deal:

add_shortcode('key_value' , 'key_value' );
function key_value( $atts, $content = null ) {
  $atts = shortcode_atts( array(
     'title' => '',
     'url'   => '',
  ), $atts );
  if ($atts['title']) {    
    $html = '<li><strong>'.$atts['title'].': </strong>'.$content.'</li>';
  } elseif ($atts['url'] && $atts['title']) {
    $html = '<li><a href="'.$atts['url'].'" target="_blank">'.$atts['title'].'</a></li>';
  }

  return $html;
}

I want to return the second if statement because I've got a bunch of shortcode like that:

[key_value title="Delivery"]Fast[/key_value]
[key_value title="Duration"]2 weeks[/key_value]

And they work fine but the last one:

[key_value title="Info Session" url="/info/"][/key_value]

doesn't work and I don't understand why because, if I var_dump my array $atts, I'm able to see both value.

Any suggestion?

1 Answer 1

1

Your code is getting into the first condition. Change the logic to execute first the else condition, or in other words, if have both attributes use it, else check only for title:

if ($atts['url'] && $atts['title']) {
    $html = '<li><a href="'.$atts['url'].'" target="_blank">'.$atts['title'].'</a></li>';
} elseif ($atts['title']) {    
    $html = '<li><strong>'.$atts['title'].': </strong>'.$content.'</li>';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.