2

I have a checked a few links but nothing seems to for me in getting the attribute value of a shortcode so that I can use it in a condition.

function bf_shortcode_slideshow($atts) {
  extract(shortcode_atts(array(
    'location' => ''
  ), $atts));

  ob_start();
  if  ( $location = 'home' ) {
  classslider = 'homeslider';
}
  include(dirname(dirname(__FILE__)) . '/templates/slideshow.php');
  return ob_get_clean();
}
add_shortcode('rotator', 'bf_shortcode_slideshow');

In the slideshow.php I have a

echo 'classslider=' . $classslider;

My result for $classslider is blank ( nothing is displayed ) Is there another way you should compare $location to a string value set ?

1 Answer 1

2

include changes the scope. You would need to declare $classslider global before defining it, and then use global $classslider; before using it.

function bf_shortcode_slideshow($atts) {
  global $classslider;
  extract(shortcode_atts(array( ...

I think that will fix your problem.

I don't understand why you are using include at all though. There must be a better way to make that slideshow work.

6
  • Thank you for your response. When I add global $classslider and then echo out $classslider in the included file, I still don't get anything returned. I believe the included file seperate from the shortcode creation is to keep the code clean.
    – thembisue
    Jan 24, 2013 at 20:46
  • Did you add global $classslider; to the included file as well?
    – s_ha_dum
    Jan 24, 2013 at 20:47
  • yes I added it global $classslider; at the start of the included file
    – thembisue
    Jan 24, 2013 at 20:49
  • Ok In addition so your solution,I made a change in how the condition was written and now it is working. the condition shouuld have been if ($location = 'home') {$classslider = "homeslider";} Thank you
    – thembisue
    Jan 24, 2013 at 20:54
  • Explaining exactly what you did might help the next person who wanders by.
    – s_ha_dum
    Jan 24, 2013 at 20:55

Your Answer

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

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