Skip to content

Create custom labels for publication lists

Michael edited this page Aug 16, 2017 · 1 revision

Since teachPress 6.1 it's possible to use custom meta data of publications directly. To show the possibilities of this feature, the following article describes the creation of custom labels for publications.

Step 1: Add custom meta data fields

Firstly, you need a custom meta data field to store the information, which publication becomes a label. To add custom meta data fields go to Settings->teachPress->Meta:Publications. For the labels you can use a checkbox group:

Add custom meta data fields

Afterwards, you can define the labels you need. The title of the option group shows also the full name of the meta field, which you need later for the data access.

Add the labels

Finally, you see the new checkboxes in the custom meta data section of the publication edit screen:

Custom meta data section

Step 2: Adding the fields to the template

In the last step, you need to add some code to the template, which creates your labels. In this example I edit get_entry() method of the tp_template_2016.php in the templates folder of the plugin directory.

Firstly, you need access to the publication data and some variables for defining the labels:

// Get the data
$data = $interface->get_data();
$label1 = '';
$label2 = '';

$interface->get_data() returns an associative array, which holds the data from the current publication, the template and shortcode settings. You can now read the values of the meta data field over $data['row']['tp_meta_pub_custom_labels']. If the name of the label is a part of the field value, we add a label for this publication:

// Define the labels
if (strpos($data['row']['tp_meta_pub_custom_labels'], 'Open Access') !== false) { 
   $label1 = '<span class="tp_pub_type" style="background-color:red;">Open Access</span>'; 
}
if (strpos($data['row']['tp_meta_pub_custom_labels'], 'Second Label') !== false) { 
   $label2 = '<span class="tp_pub_type" style="background-color:red;">Second Label</span>'; 
}

Finally, you must define the position of the labels. In this example I write the labels after the publication type labels:

$s .= '<p class="tp_pub_title">' . $interface->get_title() . ' ' . $interface->get_type() . ' ' . $interface->get_label('status', array('forthcoming') ) . $label1 . $label2 . '</p>';

The full method

This is the full edited method, which you can use to override the existing one in the file:

public function get_entry ($interface) {
        // Get the data
        $data = $interface->get_data();
        $label1 = '';
        $label2 = '';
        
        // Define the labels
        if (strpos($data['row']['tp_meta_pub_custom_labels'], 'Open Access') !== false) { 
            $label1 = '<span class="tp_pub_type" style="background-color:red;">Open Access</span>'; 
        }
        if (strpos($data['row']['tp_meta_pub_custom_labels'], 'Second Label') !== false) { 
            $label2 = '<span class="tp_pub_type" style="background-color:red;">Second Label</span>'; 
        }
        
        // Define the entry
        $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') ) . $label1 . $label2 . '</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 result

This is the result:

The result