I added the following function to the file functions.php
function contentGenerator($param) {
if($param) {
echo "Content true";
} else {
echo "Content false";
}
}
How can I call this function within a specific blog post or page?
If you are talking about a post’s/page’s content, it’s not possible. Here are three different ways of solving your problem.The first way will give you the possibility to add the generated content inside your post’s (or page’s) content. The other two will be printed in one of your template files. E.g. in your
single.php
orpage.php
.1. Shortcode
This is more or less copy/pasted from the Shortcode API examples. Place that in your
functions.php
or in one of your custom plugins.You can include the function your post’s or page’s WYSIWYG with the following syntax:
Place this in your functions.php or in one of your custom plugins:
You can also create a shortcode which encloses content. Enclosing shortcodes gives you the ability to take text as a parameter. It could be called something like this:
Read more about that at the Shortcode API documentation.
2. Page template
This will give you the ability to set a specific template file for a page (this will not work for regular posts). Here’s the documentation about Page templates.
You can base your template from an already existing template file, like
page.php
. The only requirement is that you place a multiline comment at the top of your template file like this:There isn’t really any requirement for the file name, but try not to name it so it might get picked up by WordPress through the template hierarchy (see nr 3.). If you do, WordPress might use your template for other pages as well which will give you unexpected results.
If you have done the above correctly, you should see a Template section with a dropdown under Page Attributes when you are editing or creating a page. Like this:
3. Template Hierarchy
The Template Hierarchy decides what template should be used for the current page view and does so by looking for an appropriate file by it’s name. There is a really descriptive image of the Template Hierarchy at the Codex. Please examine that one to better understand how the Template Hierarchy works.
contentGenerator($param);
simply put this code in page.php file.
If you define functions in function.php that functions can use in several template files of your theme.