0
votes

Hi I have a site with a Custom Post Type of 'Researcher', within that I have a taxonomy called 'Supervisors', and within that a meta field named 'Job Title'. I am trying to figure out how to display this 'Job Title' in the front end.

The code for the meta field (from functions.php) is as follows:

// Edit Supervisor Taxonomy

// Add term page
function jobtitle_taxonomy_add_new_meta_field() {
    // this will add the custom meta field to the add new term page
    ?>
    <div class="form-field">
        <label for="term_meta[jobtitle]"><?php _e( 'Job Title', 'jobtitle' ); ?></label>
        <input type="text" name="term_meta[jobtitle]" id="term_meta[jobtitle]" value="">
        <p class="description"><?php _e( 'Enter a value for this field','jobtitle' ); ?></p>
    </div>
<?php
}
add_action( 'supervisor_add_form_fields', 'jobtitle_taxonomy_add_new_meta_field', 10, 2 );

// Edit term page
function jobtitle_taxonomy_edit_meta_field($term) {

    // put the term ID into a variable
    $t_id = $term->term_id;

    // retrieve the existing value(s) for this meta field. This returns an array
    $term_meta = get_option( "taxonomy_$t_id" ); ?>
    <tr class="form-field">
    <th scope="row" valign="top"><label for="term_meta[jobtitle]"><?php _e( 'Job Title', 'jobtitle' ); ?></label></th>
        <td>
            <input type="text" name="term_meta[jobtitle]" id="term_meta[jobtitle]" value="<?php echo esc_attr( $term_meta['jobtitle'] ) ? esc_attr( $term_meta['jobtitle'] ) : ''; ?>">
            <p class="description"><?php _e( 'Enter a value for this field','jobtitle' ); ?></p>
        </td>
    </tr>
<?php
}
add_action( 'supervisor_edit_form_fields', 'jobtitle_taxonomy_edit_meta_field', 10, 2 );

// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
    if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['term_meta'][$key] ) ) {
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );
    }
}  
add_action( 'edited_supervisor', 'save_taxonomy_custom_meta', 10, 2 );  
add_action( 'create_supervisor', 'save_taxonomy_custom_meta', 10, 2 );

// End Edit Supervisor Taxonomy

Image showing the backend — http://d.pr/i/TS13

1

1 Answers

0
votes

You will need:

  • The post ID of the researcher. On the single-researcher.php page (if your post type is named researcher) you should be able to use $post->ID to obtain the post ID of the researcher.
  • The name of the taxonomy. This can be obtained by reading the URL of the screenshot you posted previously. It should say: ...edit-tags.php?taxonomy=supervisors&.... I'll assume the taxonomy is named supervisors.

If you're on the single-researcher.php page, this might work... Note: $supervisor_tags is an array of tags for that researcher. Each researcher could have 0, 1, or more supervisors, so you might want to loop through them:

global $post;
$supervisor_tags = get_the_terms($post->ID, 'supervisors');

echo "<h2>Supervisors:</h2>";

foreach ($supervisor_tags as $tag){
    $term_meta = get_option('taxonomy_' . $tag->term_id);
    echo "<p>Name: " . $tag->name . " - Job Title: " . $term_meta['jobtitle'] . "</p>";
}

You can echo "<pre>"; print_r($variable); echo "</pre>"; to see what the $variable array looks like, if you're having trouble.