I have a two part shortcode, with opening and closing shortcodes, in a template file. See example below.
echo do_shortcode('[myshortcode][/myshortcode]');
I see from the Codex that I can do this:
echo do_shortcode('[myshortcode]' . '<div class="anyHTML">Any Text</div>' . '[/myshortcode]');
Is it possible/correct or will it work to do this instead?
echo do_shortcode('[myshortcode]');
$somePHP = possibleWordPressLoopCodeOrOtherPHP;
echo do_shortcode('[/myshortcode]');
I'm wondering about this to see if I can include PHP inside a two part shortcode, possibly a WordPress loop for a CPT or even other PHP code.
You need to have the whole shortcode block inside the do_shortcode function. So you could do something like this:
$text = some_code_or_function_that_returns_text_your_shortcode_can_act_on();
$sc_string = sprintf("[myshortcode]\n%s\n[/myshortcode]", $text);
echo do_shortcode($sc_string);
Related
I am trying to use the content to signify which template_part code to load on my page. For example, if the content == "volume" I want to load get_template_part('lib/volume');
<?php
$content = get_the_content();
$content = strip_tags($content);
$content = html_entity_decode($content);
$content = filter_var($content, FILTER_SANITIZE_STRING);
get_template_part('lib/'.$content);
?>
This is the code in my template, however it doesn't load the desired template part.
You can strip all html tags with using wp_strip_all_tags() to get only the content. strip_tags() won't help you get rid of comments and wordpress gives us the great function to also delete the script and style.
https://developer.wordpress.org/reference/functions/wp_strip_all_tags/
So you can use one line of code to get a clean content:
$content = wp_strip_all_tags( get_the_content() );
Maybe this is the reason. Have you tried so display the value of $content to make sure you have the string you want to have? If it outputs the string the right way, there might be a problem with the get_template_part() function.
https://developer.wordpress.org/reference/functions/get_template_part/
You can try putting the string together, before using in the function:
$content = wp_strip_all_tags( get_the_content() );
$content = "lib/".$content;
get_template_part( $content );
If this is not working, check the name of the folder lib (has to be subfolder in the root of your theme folder) and the name of your php files. It could also be possible that there is something wrong with the template part file and that is the reason it is not loaded. Next check the page template inside of which you are trying to get the template part.
If nothing helps you out, it could be an alternative using include() instead of get_template_part().
When I use shortcode on page works good but on posts is displayed only shortcode name.
Example shortcode:
function foobar_func( $atts ){
return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );
Usage:
[foobar]
On page is displayed
foo and bar
but on post only
[foobar]
I tryied on basic theme like TwentySeventenn and the problem still exists.
It's very strange. So you added the above code to TwentySeventenn theme's functions.php, but it does not work on post? Please double check you write [foobar] correctly on your post content file.
Or if you changed something on the theme, please check you used the_content() instead of echo get_the_content();
shortcodes are not working if you use get_the_content();
with my plugin i want to display an image on top of a theme. the highest possibilaty is where some tags are displayed with post_class function. My question is if it's possible to hook right after the function is executed?
as you can see the content of post_class function is echo
function post_class( $class = '', $post_id = null ) {
// Separates classes with a single space, collates classes for post DIV
echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
}
you can see How to use post_class() in an echo as example below:
echo '<div ' . post_class() . '>';
you can create another filter function and calling post_class in content.
Good Luck!
I am using a plugin called: InPost Gallery. I have added the shortcode to a template file but the shortcode needs the post ID to find the images related to the page. Is it possible to get the post id added to a shortcode in a template file?
This is what I have at the moment:
<?php echo do_shortcode("[inpost_nivo slide_width='600px' slide_height='auto' thumb_width='75' thumb_height='75' post_id="28" skin='light' transition_effect='random' transition_speed='600' autoslide='5000' control_nav='1' control_nav_thumbs='1' direction_nav='1' direction_nav_hide='0' controlNavThumbs='0' random_start='0' pause_on_hover='1' show_description='1' box_rows='4' box_cols='8' slices='15' start_slide='0' id='' random='0' group='0' show_in_popup='0' album_cover='' album_cover_width='200' album_cover_height='200' popup_width='800' popup_max_height='600' popup_title='Gallery' type='nivo'][/inpost_nivo]"); ?>
You can see the post_id is set to 28. This needs to change depending on the page currently being viewed.
If this is possible it would be a great help to hear your suggestions.
Many thanks
If it's within the loop, you can for example use:
<?php
// Display shortcode for current post:
$s = sprintf( '[inpost_nivo post_id="%d"]', get_the_ID() );
echo do_shortcode( $s );
?>
You could also try to skip the post_id attribute, and see if it has the same default setup.
In my functions file I have this:
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
add_shortcode( 'caption', 'caption_shortcode' );
In the CMS page editor I have this:
[caption]My Caption[/caption]
This page is utilizing a custom template file template-mypage.php. My question is: I would like to create multiple short codes types within the CMS such as:
[caption1]My Caption[/caption1]
[caption2]My Caption[/caption2]
[caption3]My Caption[/caption3]
then in my template-mypage.php... I would like to selectively choose where to place [caption1], [caption2], [caption3]... for example [caption1] will go somewhere on the top... [caption2] in the middle and [caption3] towards the bottom of the template-mypage.php, all seperated by some huge chunks of HTML content. I do not want to write any HTML within the WP CMS... all HTML should be written in the template-mypage.php.
Currently I believe WP limits shortcode output to come out of the_content(); Is it possible to do something like the_content_of_caption1(), the_content_of_caption2(), the_content_of_caption3()?
Thanks please let me know!
this product does this perfectly
http://wordpress.org/plugins/multiple-content-blocks/