Support » Plugins » Hacks » Nivo Slider / WordPress Slug

  • I am using the Nivo Slider plugin for WordPress, but want to hard code the slider into my page and have it dynamically pull in the correct slider as the page and Nivo slider will share the same slug.

    If my slider was called Flip Clock Print this is what works…
    <?php echo do_shortcode('[nivoslider slug="flip-clock-print"]'); ?>

    If I want the slug of the page, this is what works…
    <?php echo $post->post_name; ?>

    How do I combine the two together, because I guess this isn’t right?
    <?php echo do_shortcode('[nivoslider slug="$post->post_name;"]'); ?>

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    You last snippet is not right, but it’s close. Just reverse the single and double quotes. PHP variables are only expanded when inside overall double quotes, they are treated as literal strings when inside overall single quotes. Also drop the semi-colon after the variable, it’s only used at the end of actual code lines, not each variable.

    Finally, some complex variable references need to be enclosed in curly brackets {}, but IIRC object references like -> expand OK without.

    <?php echo do_shortcode("[nivoslider slug='$post->post_name']"); ?>

    this is great. and is working well in my theme. however what would i add to this if i wanted to create a fallback slider for any post or page that does not have a specific slider assigned to it? is there some kind of elseif statement that i could use?

    Moderator bcworkz


    Probably. Unfortunately, I’m not familiar with your slider. Exactly how can we determine by code if a specific slider is assigned or not? Something like the following would work if we can determine that one critical bit:

    if( isset( $assigned-slider )) {
      echo do_shortcode("[nivoslider slug='$assigned-slider']");
    } else {
      echo do_shortcode("[nivoslider slug='default-slider']");

    Not the most concise code, but it illustrates the concept well.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Nivo Slider / WordPress Slug’ is closed to new replies.