0

I've switched on Gutenberg and am trying to create a shortcode on a page. This is my code in functions.php

// Enable shortcodes in text areas
add_filter( 'widget_text', 'shortcode_unautop');
add_filter( 'widget_text', 'do_shortcode');

// Enable shortcodes
add_filter( 'the_excerpt', 'shortcode_unautop');
add_filter( 'the_excerpt', 'do_shortcode');


function first_shortcode() { 
	$content = '<h1>This is my first shortcode</h1>';
	return $content;
}
add_action('my_shortcode','first_shortcode');

In the post, I've put [my_shortcode] in the shortcode section, as follows:

enter image description here

But when I display the page, it just renders [my_shortcode]. I'm running Wordpress 4.9.8

Thank you

4

1 回答 1

5

you are using wrong function to generate Shortcode

Use add_shortcode to create shortcode rather then add_action

Please use this code..

function first_shortcode() { 
  $content = '<h1>This is my first shortcode</h1>';
  return $content;
}
add_shortcode('my_shortcode', 'first_shortcode');
于 2018-08-16T09:39:18.277 回答