How to call function within a page/post, to dynamically generate content?

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?

Related posts

Leave a Reply

2 comments

  1. 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.
        extract(shortcode_atts(array(
            '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:

    <?php
    /*
    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.

  2. 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.