WordPress.org

Ready to get started?Download WordPress

Forums

Nivo Slider / WordPress Slug (4 posts)

  1. craig.eves
    Member
    Posted 1 year ago #

    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;"]'); ?>

  2. bcworkz
    Member
    Posted 1 year ago #

    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']"); ?>

  3. dreamraven
    Member
    Posted 1 year ago #

    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?

  4. bcworkz
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic