How to use wordpress shortcode in template - wordpress

I need help with wordpress shortcode. I have written shortcode function in functions.php in my theme files. Now I create page in wordpress administration and then I need to place this shortcode into special place in my theme but with content which I wrote in administration.
I hope that you understand what I mean.
Thank you for your help.

In your template code:
<?php echo do_shortcode('[your-shortcode]'); ?>
Replace [your-shortcode] with whatever your shortcode is, including parameters/attributes and/or closing tags if required.

Related

Add Shortcode to All Post Types

I want to display shortcodes (I'm using fruitful Shortcode) above the post title tag (H1). And I want to display it to all my post (also as a template).
i want to be like this
Thanks.
If you want to adjust the way your single posts are displayed for all the posts, you have to edit the single.php file in your theme folder. You can do this in the backend under "Design" -> "Theme Editor" or you can access the folder and files via FTP.
For your default posts in wordpress you just need to open the single.php file and look for the h1 with your title. It can look something like:
<h1><?php the_title(); ?></h1>
Above that (if you want to display it above), you can put your shortcode with the do_shortcode() function:
<?php echo do_shortcode('[name_of_shortcode]'); ?>
If you are not using normal posts of wordpress but custom post types or something else, please have a look at the wordpress template hierarchy to find out, which file to edit or how to name the new files for creating template files: https://developer.wordpress.org/themes/basics/template-hierarchy/
If you are using a theme that is not made by you, you should make sure you keep your theme updateable. So if you overwrite the single.php, with the next update of your theme, the file will not have your edits. You need a child theme, if you want to keep your parent theme up to date but also customize it in the page templates. Here is a nice tutorial for child themes: https://www.smashingmagazine.com/2016/01/create-customize-wordpress-child-theme/

Woocommerce best_selling_products shortcode not working

Trying to add:
<?php echo do_shortcode('[products best_selling="true" limit="4"]'); ?>
In a template returns nothing. I see that no hooks are present for this but woocommerce says it should be supported.
Please Try this shortcode
[best_selling_products per_page="12"]
Please refer below link for Woocommerce Shortcodes
https://docs.woocommerce.com/document/woocommerce-shortcodes/
Coming back to this one because I came across another shortcode that wasn't working.
It turned out that the theme was the culprit. After switching to the default theme all shortcodes were working correctly.

Show all post content on Wordpress

Im using the theme HTML 5 Blank (http://html5blank.com/) with WordPress 3.7.1.
On my page I want to display everthing from the posts, text and images.
In the settings in wordpress admin i have it set on show hole post but it does not work.
I have olso deleted the php code in the loop that inserts the excerpt from the theme. Sitt not workning. Any ideas? I can send code if you want, for now i do not know what code can be usefull.
Jim
Replace <?php the_excerpt() ?>and add the following code to your theme's loop.
<?php the_content(); ?>
That should display the content, rather that the excerpt.

Wordpress plugin shortscodes in other plugins?

I'm developing a plugin that creates custom wordpress pages, however I ran into a problem when trying to get shortcodes from other plugins to integrate into my plugin, well more like I couldn't find any info on it.
For example if someone uses my custom page builder and adds a short code [gallery] that is associated with an enabled plugin, I want the shortcode to do it's thing.
Could anyone point me in the right direction for this?
Try wordpress
http://wordpress.org/plugins/synved-shortcodes/
while this plugin let's you integrate shortcodes on every wordpress page , I suppose that you issue is with wordpress functions.php file where you can have a look at , with this plugin installed !
You can check if a shortcode exist before printing it with the function shortcode_exists() (https://codex.wordpress.org/Function_Reference/shortcode_exists)
Example:
<?php
if ( shortcode_exists( 'gallery' ) ) {
// The [gallery] short code exists.
}
?>
Then, if it exist and you want to print that shortcode with PHP code you should use do_shortcode() function (https://developer.wordpress.org/reference/functions/do_shortcode/)
Example:
<?php echo do_shortcode('[gallery]); ?>
Always use in WordPress's in-built
the_content
hook to print your custom page content.
WordPress by default process all shortcodes before outputting any stuff from this hook.
More info can be found here: https://codex.wordpress.org/Function_Reference/the_content

How to include a Wordpress Shortcode in your Code, not in the Posts Section

I want to insert a hardcoded short code in my code, and not from the usual Text Editor we usually use.
Basically I want this to add a gallery, and the user doesn't need to change the shortcode from the CMS so I will be hardcoding this.
How would I need to do this, I tried to just post it in my .php file but it doesn't work.
This is the code I want to add:
[jj-ngg-jquery-slider gallery="1" width="866" height="341" ]
This will do the trick to include in .php files:
<?php echo do_shortcode('[jj-ngg-jquery-slider gallery="1" width="866" height="341"]'); ?>
shortcodes were created to include in post or pages. I could be wrong but wordpress checks the input of a post and if it finds a shortcode it will replace it with the html. I don't think it will work if you add shortcodes in your .php file because wordpress doesn't look for shortcodes in your php files
You could just create a function in functions.php to generate the html you need. Then you just call that function within your theme .php file. That's how most plugins are made. Shortcode for post & pages and function in the php files.
example:
<?php echo myGallery(array('gallery'=>1, 'width'=>866, 'height' => 341); ?>
Did you try this method ? do_shortcode($content)
I've seen it on http://codex.wordpress.org/Shortcode_API

Resources