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

Read More

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?

Related posts

Leave a Reply

1 comment

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