Search code examples
phpwordpressshortcode

Wordpress do_shortcode function while calling the_slug to replace the category attribute


I am trying to use this shortcode in my theme and I want the category to become the slug for whatever page it's on.

<?php echo do_shortcode('[image-carousel category="'.the_slug().'"]'); ?>

Now while this does achieve that function, it is also spitting out the actual text of the slug right above the image. I tried using variables, also tried escaping the quotes, and a bunch of other stuff yet the above script is the only way I've gotten it to display the proper image, the only problem is the visible text.

How can I avoid that from happening?


Solution

  • All functions that start with the_ will echo its results, and you don't want that within a do_shortcode().

    Use:

    $slug = get_post( $optional_id )->post_name;
    echo do_shortcode('[image-carousel category="'.$slug.'"]');