03. Adding Custom Section Templates

Custom Section Templates instruct Print My Blog Pro to display a particular post, page, or other post type, differently.

The built-in section templates are:

  • “Default” which varies depending on whether the section is an article, a part, a volume or anthology. But it’s whatever your design decides is “normal”
  • “Just Content” leaves out the post’s title and other metadata, and only places its contents on the page, starting at the top
  • “Centered Content” is like “Just Content” except its content is placed in the middle of the page.

To add another section template, you’ll need to create a simple plugin containing some initialization code and a template file.

Check out the Print My Blog – New Section Template GitHub repository. You can download or checkout that project and place it in your wp-content/plugins folder to see it in action. I adds a new section template called “Sideways” (for the classic print and digital PDF designs) which just displays the post’s title sideways.

The plugin is initialized by the file pmb-new-section-template. It contains:

<?php
/*
Plugin Name: Print My Blog - New Section Template
Plugin URI: https://github.com/mnelson4/pmb-sample-default-template-override
Description: https://github.com/mnelson4/pmb-new-section-template
Author: Mike Nelson
Version: 1.0.0
Author URI: https://printmy.blog
*/

define('PMBNST_MAIN_FILE', __FILE__);
define('PMBNST_MAIN_DIR', __DIR__);
add_action( 'pmb_register_designs', 'pmbnst_register_section_template', 1 );

function pmbnst_register_section_template(){
    pmb_register_section_template(
        'sideways',
        [
            'classic_print',
            'classic_digital'
        ],
        function(){
            return [
                'title' => __('Sideways Title', 'print-my-blog'),
                'fallback' => '',
                'filepath' => PMBNST_MAIN_DIR . '/sideways.php'
            ];
        }
    );
}

The function pmb_register_section_template is located in Print My Blog’s inc/integration_functions.php. It accepts the new template’s slug, then a list of all the design template it applies to (for example, if you also wanted to use this from the Buurma Whitepaper and Mayer Magazine, you would add buurma and mayer to that list), and lastly a function that returns an array of details about the new section template. That array should contain

  • title (to be displayed to the project editor)
  • fallback (the template to use when a project’s section is using this template, but the design doesn’t support it). The empty string means to fallback to the PMB default.
  • filepath is the absolute filepath on the server of the new file.

The file sideways.php is the actual template file. It contains

<?php
/**
 * @var \PrintMyBlog\orm\entities\Project $pmb_project
 * @var PrintMyBlog\orm\entities\Design $pmb_design
 */
?>
<div <?php pmb_section_wrapper_class();?> <?php pmb_section_wrapper_id();?>>
    <article <?php pmb_section_class(); ?> <?php pmb_section_id(); ?>>
        <header class="entry-header has-text-align-center">

            <div class="entry-header-inner section-inner medium" style="transform: rotate(-90deg);">
                <?php pmb_the_title();?>
            </div><!-- .entry-header-inner -->
        </header><!-- .entry-header -->
    </article>
<?php // end of file. For some reason this comment was needed to prevent a fatal parsing error on dev.printmy.blog

There’s a PHP comment at the top saying the variables $pmb_project and $pmb_design are both available, and specifying what type of objects they are. Also note that the global $post has been set, so you can use all of WordPress’ standard template tags like the_content() and the_meta() if you want.

Also note that the file has an unclosed div. That’s on purpose. The div tag is closed later automatically.