S. Brown
S. Brown

Reputation: 41

Wordpress template Echo a php string

I'm working on a page template and I'm having trouble working out how I can echo a string of php. Basically I have set up a custom field inside Wordpress for the page called Slider Code, and it asks the user to paste in the slider code given to them so that the appropriate slider is displayed on the page. The slider code given looks like this: [soliloquy id="82"]

The custom field plugin gives me this code to use in the template to echo out whatever is in that field, which looks like this: <?php echo CFS()->get('slider_code'); ?>

In WordPress there is a php function called do_shortcode which enables me to use that code, so it would look like this: <?php do_shortcode('[soliloquy id="82"]'); ?> and the slider would be displayed. So is there any way I can merge that <?php echo CFS()->get('slider_code'); ?> string with the do_Shortcode string? If not, does anyone have any alternative solutions to this issue? I can only think of having to make separate page templates for each individual slider.

EDIT: I have just discovered that the do_shortcode function does not actually work at all with the soliloquy shortcode. It does give me the option of using this code: if ( function_exists( 'soliloquy' ) ) { soliloquy( '82' ); }. However, when I paste this code into the custom field as is, the <?php echo CFS()->get('slider_code'); ?> tag just echos out that text. If I wrap the code in the custom field in <?php and ?> it just comments the code out inside my inspector. How do I get around this?

Upvotes: 0

Views: 925

Answers (1)

John Fonseka
John Fonseka

Reputation: 795

Check this out.

$slider = CFS()->get('slider_code');
call_user_func('do_shortcode', $slider);

I don't understand why you can't just run do_shortcode(CFS()->get('slider_code')).

Ok. Then try this one. Paste this code in the template file.

<?php if ( function_exists( 'soliloquy' ) ) { soliloquy( CFS()->get('slider_code') ); } ?>

And for slider_code input just enter '82'. If you are unable to see what is the template file you have to paste the above given code, then try to see WordPress template conventions.

Upvotes: 1

Related Questions