1

I'm using this function right now in my theme to show each project's custom taxonomy. In this case, the custom taxonomy is tipo_de_tarea:

echo get_the_term_list( $post->ID, 'tipo_de_tarea' );

I have various tipo_de_tareas. I would like the function to display a shorter name, instead of the complete name of each taxonomy, for example:

  • Apuntes => AP
  • Ejercicios => EJ

As I show in this example, the left side has the current design, and the right has the wished:

enter image description here

I guess that I need to add if conditions for each tipo de tarea and the wished name, but how can I do this? I don't know the code for this.

Also, I would like each one to have a different class that can be assigned a different background color for each one.

2 Answers 2

1

You could use get_terms() and loop through it - using the term description as the "short name" and slug as the specific class ( or you could use an array of colors of your choice ). Let's look at a quick example:

<?php
    $terms = wp_get_post_terms( $post_id, 'category' );

    if( ! empty( $terms ) ) : ?>

        <?php foreach( $terms as $term ) : ?>

            <div class="<?php echo $term->slug; ?>"><?php echo $term->description; ?></div>

        <?php endforeach; ?>

<?php
    endif;
?>

If you wanted to make a list out of it or link from it you have full control over the HTML - let me know if you have questions about the markup.

0

Thanks for your help. I found finally help in another place and I'm now using these functions in functions.php:

    // A callback function to add a custom field to our "presenters" taxonomy  
function presenters_taxonomy_custom_fields($tag) {  
   // Check for existing taxonomy meta for the term you're editing  
    $t_id = $tag->term_id; // Get the ID of the term you're editing  
    $term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check  
?>  

<tr class="form-field">  
    <th scope="row" valign="top">  
        <label for="abreviatura"><?php _e('Abreviatura'); ?></label>  
    </th>  
    <td>  
        <input type="text" name="term_meta[abreviatura]" id="term_meta[abreviatura]" size="25" style="width:60%;" value="<?php echo $term_meta['abreviatura'] ? $term_meta['abreviatura'] : ''; ?>"><br />  
        <span class="description"><?php _e('Abreviatura del tipo de tarea'); ?></span>  
    </td>  
</tr>  

<?php  
}  

// A callback function to save our extra taxonomy field(s)  
function save_taxonomy_custom_fields( $term_id ) {  
    if ( isset( $_POST['term_meta'] ) ) {  
        $t_id = $term_id;  
        $term_meta = get_option( "taxonomy_term_$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_term_$t_id", $term_meta );  
    }  
}  

// Add the fields to the "presenters" taxonomy, using our callback function  
add_action( 'tipo_de_tarea_edit_form_fields', 'presenters_taxonomy_custom_fields', 10, 2 );  

// Save the changes made on the "presenters" taxonomy, using our callback function  
add_action( 'edited_tipo_de_tarea', 'save_taxonomy_custom_fields', 10, 2 );  

And I call the code with:

    <?php
$terms = get_the_terms( $post->ID, 'tipo_de_tarea' );
if ($terms && !is_wp_error($terms)): 
    foreach($terms as $term): ?>
        <a href="<?php echo get_term_link( $term->slug, 'tipo_de_tarea'); ?>" rel="tag" class="<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>"><?php echo $term->abreviatura; ?></a>
    <?php
    endforeach;
endif; 
?>

But there is some problem with the functions and I only see a blank column...

Anyone knows where is the problem?

Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.