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?

2 Answers 2


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 or page.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:

[unique_shortcode_name foo="123" bar="medium"]

Place this in your functions.php or in one of your custom plugins:

add_shortcode('unique_shortcode_name', 'generate_content');
function generate_content($attributes) {
    // Parses the parameters/attributes and makes them available as variables.
        'foo' => 'default value for foo',
        'bar' => 'default value for bar',
    ), $attributes));

    // Generate stuff
    $output = complex_content_generation_goes_here();

    return $output;

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:

[unique_shortcode_name foo="123"]Some text input[/unique_shortcode_name]

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:

Template Name: Products Page

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:

Image showing the template settings on a page edit

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.


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.

  • I only want to add the function to a single page (the "About me" page) and not to all pages. I want to call the function at a certain position of that page. I tried to call the function by adding PHP commands within the HTML Source using Test function is <?php contentGenerator($true); ?> ., but this did not work.
    – Sven
    Oct 9, 2012 at 6:24
  • 1
    If you are talking about a post's/page's content, it's not possible. What you can do is to add a shortcode and call that. You could also have a specific template file or use the template hierarchy for that page.
    – hampusn
    Oct 9, 2012 at 6:36
  • @hampusn please move that info to a separate answer
    – Rarst
    Oct 10, 2012 at 0:45
  • @Rarst Sure thing. I didn't have the time to write a more extended answer but will have one ready in a short time.
    – hampusn
    Oct 10, 2012 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.