There might be times when you want to associate different shortcodes with different posts and have these run on the frontend.


Let’s say you’ve created a custom field called shortcode and in Post A, set [rev_slider alias="web-product-light-hero3"] as its value and in Post B, [rev_slider alias="photography-carousel5"].

Here’s the PHP code to execute these:

$shortcode = get_post_meta( get_the_ID(), 'shortcode', true );

echo do_shortcode( "{$shortcode}" );

If you want to enter just the name of the slider (in this example) for the custom field’s value like this: web-product-light-hero3 and photography-carousel5,

the code will then become:

$shortcode = get_post_meta( get_the_ID(), 'shortcode', true );

echo do_shortcode( "[rev_slider alias='{$shortcode}']" );

Update: When the custom fields were created with Toolset, for some reason get_post_meta() isn’t working during my testing.

So use

$shortcode = types_render_field( 'shortcode', array() );

instead of

$shortcode = get_post_meta( get_the_ID(), 'shortcode', true );

Need help implementing a tutorial in your site or want to hire me for custom work?


Find the article helpful and wish to donate?

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
%d bloggers like this: