Is it possible to call a function from functions.php in custom page or blog post?
I put simple function in functions.php:
function testTest()
{
echo "Test";
}
And called it from the page:
<?php testTest();?>
But it doesn't work. Do I need to make a plugin to use simple function like that inside one chosen custom page?
Thanks for your answer, Mary
Is it possible to call a function from functions.php in custom page or blog post?
I put simple function in functions.php:
function testTest()
{
echo "Test";
}
And called it from the page:
<?php testTest();?>
But it doesn't work. Do I need to make a plugin to use simple function like that inside one chosen custom page?
Thanks for your answer, Mary
Share Improve this question asked Jul 2, 2019 at 8:26 MaryMary 451 gold badge2 silver badges5 bronze badges2 Answers
Reset to default 3You could use add_shortcode if you want to use it within the editor.
function footag_func() {
return "Test";
}
add_shortcode( 'footag', 'footag_func' );
And then use [footag] in your editor.
Or
Use code like this in functions.php and add a conditional tag
add_action( 'loop_start', 'your_function' );
function your_function() {
if ( is_singular('post') ) {
echo 'Test';
}
}
or
Create a function in functions.php
function your_function() {
return 'Test';
}
And then use this in your template
echo your_function();
You can quickly create a quick shortcode for doing that.
add_shortcode( 'test_shortcode', 'my_test_callback' );
Then, in the callback function you do this:
function my_test_callback() {
//start adding the echoed content to the output buffer
ob_start();
//run your code here - in this case your testTest() function
testTest();
//return the output buffer
//NOTE: directly echoing the content will give unexpected results
return ob_get_clean();
}
Then, in your content pages you just add [test_shortcode]
and it will run your PHP function.
For a better view on shortcodes here are some useful links:
Official Shortcode API
A nice tool for creating shortcodes
An article I wrote on how to build shortcodes
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745352477a4623917.html
评论列表(0条)