0

I've changed my functions.php file adding a new taxonomy called "autor". Here the code:

add_action( 'init', 'create_autor_nonhierarchical_taxonomy', 0 ); function create_autor_nonhierarchical_taxonomy() {
// Labels part for the GUI
$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' ),
); 

// Now register the non-hierarchical taxonomy like tag

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( 'woocommerce_single_product_summary', 'show_product_autor', 24 );

function show_product_autor(){

the_terms( $post->ID, 'autor', '<b>AUTOR: </b>', ', ', '<br />' ); 
}

Everything works perfectly but I'd like that the link that appears in the author's name would redirect to the author page (team plugin) and not to the shop page. How could I edit my code in order to links my taxonomy to the team page? For instance, in this page https://www.editorialufv.es/catalogo/territory-inhabited/ if you click on the name of the author, Elena Farini, you'll be redirect to his books (in this case only one) and not to his team page:https://www.editorialufv.es/team/elena-farini/

0

I see two ways to do this:

Automatically: by Matching AUTOR term name to TEAM post title

Assuming the autor's terms are the same as the custom post type team post titles, you could find the relation with code like:

function show_product_autor(){

    // get this woo-comm's product author
    $authors = wp_get_post_terms( get_the_ID() , 'autor' );

    // we know it'll just be one author, so use first object of array
    $author = array_pop($authors);

    // knowing the authors name, lets find the TEAM page
    $authorTeamPg = get_page_by_title( $author->name, 'OBJECT', 'team' );

    // now we know the authors page
    $authorTeamPgLink = get_permalink( $authorTeamPg->ID);

    // output
    echo "Author: <a href='{$authorTeamPgLink}'>{$author->name}</a>";
}

The above has no error handling for if a match was not found, but it's easy enough to code in. However, in your example you sent, the term name is Elena Farini and the custom post type team is Farini Elena. So this code would fail you unless you ensure the names match in the backend. The alternative (so you don't need to make sure of text matching), is to work with ID's and manually make the relation from term to team member, so:

Manually: by assigning a TEAM ID to an AUTOR's term meta

For autor you need to use _term_meta and create a drop-down field listing authors with value of their IDs. That will create a relation from an autor term's, to the ID of a team member.

Here's a good article on setting up the term meta feilds.

Once setup, in show_product_autor() you'd do somthing like this:

function show_product_autor(){

    // get this woo-comm's product author
    $authors = wp_get_post_terms( $post->ID, 'autor' );

    // we know it'll just be one author, so use first object of array
    $author = array_pop($authors);

    // get the team page ID from _term_meta for this term author
    $authorTeamID = get_term_meta( $author->term_id, 'autor', true );

    // get the full TEAM page for this author
    $authorTeamPg = get_post( $authorTeamID );

    // now we know the authors page
    $authorTeamPgLink = get_permalink( $authorTeamPg->ID);

    // output
    echo "Author: <a href='{$authorTeamPgLink}'>{$authorTeamPg->post_title}</a>";
}
2
  • Thank you David. I've changed the name of the team members in order to have the same name both in the product page and in the team page. Nevertheless the name of the author doesn't appear. Is there any error in the php code? Take a look to the product page: editorialufv.es/catalogo/territory-inhabited – Stefano Dec 22 '17 at 14:06
  • hm. i used $post->ID instead of get_the_ID(), which may of caused an error since the global $post wasn't set. – David Sword Dec 22 '17 at 17:55

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.