1

I've add a custom taxonomy in my function.php. Here the code:

function create_autor_nonhierarchical_taxonomy() {

    $labels = array(
        'name' => _x( 'Autor', 'taxonomy general name' ),
        'singular_name' => _x( 'Autor', 'taxonomy singular name' ),
        'search_items' =>  __( 'Buscar autores' ),
        'popular_items' => __( 'Autores populares' ),
        'all_items' => __( 'Todos los autores' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Editar autor' ), 
        'update_item' => __( 'Actualizar autor' ),
        'add_new_item' => __( 'Añadir nuevo autor' ),
        'new_item_name' => __( 'Nombre del nuevo autor' ),
        'separate_items_with_commas' => __( 'Separa los autores con comas' ),
        'add_or_remove_items' => __( 'Añadir o eliminar autores' ),
        'choose_from_most_used' => __( 'Elije ente los autores más utilizados' ),
        'menu_name' => __( 'Autor' ),
    ); 

    register_taxonomy( 'autor', 'product', array(
        'hierarchical' => false,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var' => true,
        'rewrite' => array( 'slug' => 'autor' ),
    ));
}

add_action( 'init', 'create_autor_nonhierarchical_taxonomy', 0 );

function show_product_autor(){

    $authors = wp_get_post_terms( get_the_ID(), 'autor' );
    $author = array_pop($authors);

    $authorTeamPg = get_page_by_title( $author->name, 'OBJECT', 'team' );
    $authorTeamPgLink = get_permalink( $authorTeamPg->ID);

    echo "<b>AUTOR: </b><a href='{$authorTeamPgLink}'>{$author->name}</a>",'<br />';

}

add_action( 'woocommerce_single_product_summary', 'show_product_autor', 24 );

Now, I've books that have more than 1 author. How could I change the code in order to display for instance 2 or 3 authors? Moreover there are books that have an editor, not properly an author (a books that have many authors and only one is in charge of the publication). Normally in this case the name of the editor is followed by a parenthesis like this (ed.). How could I add this option to the author field?

  • Any idea? I'm completely lost :( – Stefano Dec 25 '17 at 9:53
  • are editors always editors or can they be both author on one book and editor on another? – inarilo Dec 25 '17 at 11:30
  • Author on one book and editor on another. This makes things much more complicated :( – Stefano Dec 25 '17 at 11:31
  • Do you consider installing ACF plugin or it have to be taxonomies? – kierzniak Dec 25 '17 at 12:10
  • I've already installer it but still don't know how it could help me find the solution :( – Stefano Dec 25 '17 at 12:11
2

To display multiple authors, loop through the array of authors:

function show_product_autor(){    
    $authors = wp_get_post_terms( get_the_ID(), 'autor' );

    foreach($authors as $author) {
        $authorTeamPg = get_page_by_title( $author->name, 'OBJECT', 'team' );
        $authorTeamPgLink = get_permalink( $authorTeamPg->ID);
        echo "<b>AUTOR: </b><a href='{$authorTeamPgLink}'>{$author->name}</a>",'<br />';
    }
}

To handle editors, the simplest solution IMO would be to make the taxonomy heirarchical and add a term editor under all authors who are also editors. When selecting editors, select both author name and the 'editor' term under it.

So the above code becomes:

function show_product_autor(){    
    $authors = wp_get_post_terms( get_the_ID(), 'autor');
    $output  = array();
    foreach($authors as $author) {
        if(!$author->parent) { //if there is no parent term
            $authorTeamPg = get_page_by_title( $author->name, 'OBJECT', 'team' );
            $authorTeamPgLink = get_permalink( $authorTeamPg->ID);
            $output[$author->term_id]['url'] = "<a href='{$authorTeamPgLink}'>{$author->name}</a>";
        } else {
            $output[$author->parent]['ed'] = ' (ed.)';
        }
    }
    $outputfinal = array();
    foreach($output as $line) {
        if(!empty($line['url'])) { //just to be safe, check the url is there
            $outputfinal[] = (empty($line['ed'])) ? $line['url'] : $line['url'].$line['ed'];
        }
    }
    echo '<b>AUTOR: </b>'.implode(', ', $outputfinal).'<br />';
}
  • It doesn't work. I've made the taxonomy hierarchical and I've added an author called "editor" and I've subordinated it to the name of the author. Nevertheless a)now appear 2 authors (one called editor and the other called Elena Farini) editorialufv.es/catalogo/territory-inhabited; b) the link of the author stopped working; 3) in case of 2 authors it would be great to have them on the same line separated by a "," and not on 2 or more lines each one called "AUTOR:"... editorialufv.es/catalogo/… Is there a way to fix that? – Stefano Dec 25 '17 at 18:30
  • which code did you use, and did you use it as given or make changes? – inarilo Dec 25 '17 at 22:39
  • it looks like you used the second block but forgot to change the args parameter of wp_get_post_terms. – inarilo Dec 25 '17 at 22:45
  • You're right. Now I'm using the original second block but there are some problems: 1) I should add a </ br> in order to separate the author taxonomy from the others; 2) the links of the authors don't work; 3) the editor appears like the name of an author editorialufv.es/catalogo/territory-inhabited 4) It would be great to have the 2 authors on the same line separated by a "," editorialufv.es/catalogo/… – Stefano Dec 26 '17 at 7:48
  • 1
    Let us continue this discussion in chat. – inarilo Dec 26 '17 at 12:02

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.