Skip to content
Michael edited this page Apr 10, 2018 · 7 revisions

Introduction

This article is about developing templates for teachPress based publication lists. The templates can be used with the shortcodes tpcloud, tplist and tpsearch. The template support was introduced with teachPress 6.0 to create a better way to customize publication lists without changes on the plugin itself.

Please note that changes of the templates or new templates will be overridden with an automatic update. But it's possible to change the directory for the templates.

Anatomy of a template

Templates live in a sub-directory of the teachPress plugin directory (wp-content/plugins/teachpress/templates/ by default) which can be moved. teachPress includes three default templates in each new installation. Examine the files carefully to get a better idea of how to build your own templates.

The template directory holds all needed files for the templates. At least you need a PHP file and a CSS file for a new template. For example:

my_template.php
my_template.css

The basic structure of the PHP file

A teachPress template is a single PHP class, which implements the interface tp_publication_template. Thereby the basic structure for all templates is given:

Important: The name of the class must be the same as for the files. For example: If you have a my_template.php and my_template.css, you need a my_template class.

class my_template implements tp_publication_template {
    /**
     * Returns the settings of the template
     */
    public function get_settings() {
    }
    
    /**
     * Returns the body element for a publication list
     */
    public function get_body ($content, $args = array() ) {
    }
    
    /**
     * Returns the headline for a publication list or a part of that
     */
    public function get_headline ($content, $args = array()) {
    }
    
    /**
     * Returns the headline (second level) for a publication list or a part of that
     */
    public function get_headline_sl ($content, $args = array()) {
    }
    
    /**
     * Returns the single entry of a publication list
     */
    public function get_entry ($interface) {
    }
}

1. get_settings()

This function defines some basics for the template. The six array keys are required, but citation_style is currently not used. If you need more settings, you can add this to the array.

/**
 * Returns the settings of the template
 * @return array
 */
public function get_settings() {
    return array ('name' => 'My Template',
                  'description' => 'My cool new template is awesome',
                  'author' => 'Thomas Smith',
                  'version'=> '1.0',
                  'button_separator' => ' | ',
                  'citation_style' => 'teachPress'
    );
}

2. get_body()

This function defines the container for the publication list. In this example it's a table. You can change that if you want. At least the function must return the $content variable.

/**
 * Returns the body element for a publication list
 * @param string $content   The content of the publication list itself
 * @param array $args       An array with some basic settings for the publication list 
 * @return string
 */
 public function get_body ($content, $args = array() ) {
     return '<table class="teachpress_publication_list">' . $content . '</table>';
 }

3. get_headline()

This function defines the headlines in the publication list. That means the years or the publication types. In this example it's a single table row. You can change that if you want. At least the function should return the $content variable.

/**
 * Returns the headline for a publication list or a part of that
 * @param type $content     The content of the headline
 * @param type $args        An array with some basic settings for the publication list (source: shortcode settings)
 * @return string
 */
 public function get_headline ($content, $args = array()) {
     return '<tr>
                 <td' . $args['colspan'] . '>
                     <h3 class="tp_h3" id="tp_h3_' . esc_attr($content) .'">' . $content . '</h3>
                 </td>
             </tr>';
 }

4. get_headline_sl()

This function defines the second level headlines in the publication list. That means the years or the publication types. In this example it's a single table row. You can change that if you want. At least the function should return the $content variable.

/**
 * Returns the headline (second level) for a publication list or a part of that
 * @param type $content     The content of the headline
 * @param type $args        An array with some basic settings for the publication list (source: shortcode settings)
 * @return string
 */
 public function get_headline_sl ($content, $args = array()) {
     echo $args;
     return '<tr>
                 <td' . $args['colspan'] . '>
                     <h4 class="tp_h4" id="tp_h4_' . esc_attr($content) .'">' . $content . '</h4>
                 </td>
             </tr>';
 }

5. get_entry()

This function is the heart of a template and defines the design of each entry in the publication list. The $interface object gives you access to some data and functions, which you can use to create your template.

/**
 * Returns the single entry of a publication list
 * 
 * Contents of the interface object:
 *   'row'               => An array of the related publication data
 *   'title'             => The title of the publication (completely prepared for HTML output)
 *   'images'            => The images array (HTML code for left, bottom, right)
 *   'tag_line'          => The HTML tag string
 *   'settings'          => The settings array (shortcode options)
 *   'counter'           => The publication counter (integer)
 *   'all_authors'       => The prepared author string
 *   'keywords'          => An array of related keywords or tags
 *   'container_id'      => The ID of the HTML container
 *   'template_settings' => The template settings array (name, description, author, citation_style)
 * 
 * @param object $interface     The interface object
 * @return string
 */
public function get_entry ($interface) {
    $s = '<tr class="tp_publication">';
    $s .= $interface->get_number('<td class="tp_pub_number">', '.</td>');
    $s .= $interface->get_images('left');
    $s .= '<td class="tp_pub_info">';
    $s .= $interface->get_author('<p class="tp_pub_author">', '</p>');
    $s .= '<p class="tp_pub_title">' . $interface->get_title() . ' ' . $interface->get_type() . ' ' . $interface->get_label('status', array('forthcoming') ) . '</p>';
    $s .= '<p class="tp_pub_additional">' . $interface->get_meta() . '</p>';
    $s .= '<p class="tp_pub_tags">' . $interface->get_tag_line() . '</p>';
    $s .= $interface->get_infocontainer();
    $s .= $interface->get_images('bottom');
    $s .= '</td>';
    $s .= $interface->get_images('right');
    $s .= '</tr>';
    return $s;
}

The basic structure of the CSS file

There are no requirements for this file. Feel free to add all you want for your template! Just some ideas:

td.tp_pub_info {border-bottom:1px solid silver; vertical-align:top; padding:8px;}
.tp_pub_author, #content p.tp_pub_author {font-size:small; margin-bottom:1px; margin-top:1px;}
.tp_pub_title, #content p.tp_pub_title {font-size:small; font-weight:bold; margin-top:1px; margin-bottom:1px;}
.tp_pub_additional, #content p.tp_pub_additional {font-size:small; margin-top:1px; margin-bottom:1px;}
.tp_pub_tags, #content p.tp_pub_tags  {font-size:small; margin-top:1px; margin-bottom:1px; color:#AAAAAA;}
.tp_pub_tags a {color:#AAAAAA; text-decoration:underline;}
.tp_pub_tags a:hover {color:#AAAAAA; text-decoration:none;}
.tp_pub_type {background-color: #008bd2; color: #fff; display: inline-block; padding: 3px 4px; margin-left: 5px; font-size: 10px; font-weight: bold; line-height: 1; border-radius: 2px; box-shadow: inset 0 -1px 0 rgba(0,0,0,0.12);}
.tp_pub_label_status {background-color: orange; color: #fff; display: inline-block; padding: 3px 4px; margin-left: 5px; font-size: 10px; font-weight: bold; line-height: 1; border-radius: 2px; box-shadow: inset 0 -1px 0 rgba(0,0,0,0.12);}

Using the new template

Your new template can be used by changing the template parameter in the shortcodes. For example:

[tplist template="my_template"]
[tpcloud template="my_template"]
[tpsearch template="my_template"]