There might be times when you want to associate different shortcodes with different posts and have these run on the frontend.
Example:
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 );