Support » Plugin: Responsive Slider by MetaSlider - Slider and Carousel Plugin for WordPress » Just return the images included in the slider

  • Resolved ttremain

    (@ttremain)


    I wish to get the images that will be used in a particular slider. I wish to get these while the header is still being built, so I can add the URLs to Open Graph.

    Please advise.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor kbatdorf

    (@kbat82)

    Hi,

    MetaSlider doesn’t have that functionality available. You would have to write some custom PHP code to get those image urls.

    It would require some PHP knowledge to implement this. If you’re unsure about that, you’re better off finding a developer for help, or looking for another approach. We can’t provide support for custom code like this.

    You can try adding this code, but it’s using the internal API and so could change some day (probably not likely as long as I’m around, but still a possibility)! I added a try/catch statement in case it fails.

    You might want to limit it to specific pages too.

    add_action('wp_head', function () {
        if (is_admin()) return;
        try {
            $slideshow = MetaSlider_Slideshows::get_instance();
            $slideshow_array = $slideshow->get_single(53);
            $slide_ids = $slideshow_array[0]['slides'];
            $slides = array_map(function ($slide) {
                return get_the_post_thumbnail_url($slide);
            }, $slide_ids);
            //var_dump($slides);
        } catch (Exception $e) {
            // Do something if the above fails
        }
    });

    Perfect! Here is what I ended with:

    
    if ($thePage=='home' && class_exists( 'MetaSliderPlugin' ) ) {
      try {
        $slideshow = MetaSlider_Slideshows::get_instance();
        $slideshow_array = $slideshow->get_single(1937);
        $slide_ids = $slideshow_array[0]['slides'];
        $slides = array_map(function ($slide) {
          return get_the_post_thumbnail_url($slide);
        }, $slide_ids);
        foreach ( $slides as $slide ) {
          echo "<meta property='og:image' content='{$slide}'>\n";
        }
      } catch (Exception $e) {
        //Nothing to put here
      }
    }
    

    I could grab the slider ID dynamically, but no real need at this time. At least if I add or remove images down the road, I now won’t have to remember to change the OpenGraph code accordingly.

    Thank you!

    Plugin Contributor kbatdorf

    (@kbat82)

    Hi,

    Great to hear! I’ll mark this as resolved then.

    ​By the way, if you’re enjoying MetaSlider please leave us a 5-star review. They really help us out and let others find our plugin more easily.
    ​If you don’t think we deserve the 5 stars please let us know how we can improve, too. Thanks!

    https://wordpress.org/support/plugin/ml-slider/reviews/?rate=5#new-post

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.