Add custom field to wordpress taxonomy

1

I'm giving maintenance on a Wordpress site where there is a page called "Services" and so there is also the "Services Category", on certain page of the site, these services are listed within each category.

I'm trying to add a new field to "Service Category", the "sorting" so that the customer can choose the order in which the categories and their services will be listed in the format 0 for first, 1 for second and so on.

Well, I saw that a Taxonomy was created for this page but I can not get the additional field I want displayed. Here is the taxonomy code:

function taxonomiaCategoriaServico() {

$rotulosCategoriaServico = array(
                                    'name'              => 'Categorias de Servico',
                                    'singular_name'     => 'Categoria de Servico',
                                    'search_items'      => 'Buscar categorias de Servico',
                                    'all_items'         => 'Todas categorias de Servico',
                                    'parent_item'       => 'Categoria de Servico pai',
                                    'parent_item_colon' => 'Categoria de Servico pai:',
                                    'edit_item'         => 'Editar categoria de Servico',
                                    'update_item'       => 'Atualizar categoria de Servico',
                                    'add_new_item'      => 'Nova categoria de Servico',
                                    'new_item_name'     => 'Nova categoria',
                                    'menu_name'         => 'Categorias de Servico',
                                );

$argsCategoriaServico       = array(
                                    'hierarchical'      => true,
                                    'labels'            => $rotulosCategoriaServico,
                                    'show_ui'           => true,
                                    'show_admin_column' => true,
                                    'query_var'         => true,
                                    'rewrite'           => array( 'slug' => 'categoria-servico' ),
                                );

register_taxonomy( 'categoriaServico', array( 'servico' ), $argsCategoriaServico );

}

I have tried to put everything inside the label, I already saw the documentation in the codex in the page of Wordpress and I could not.

Here's how I call the template to show the information.

<?php
                    $conteudoTaxonomias = get_terms( 'categoriaServico', '' );
                    foreach ($conteudoTaxonomias as $conteudoTaxonomia):
                            $foto = $conteudoTaxonomia->description; ?>
                            <div class="col-md-6 serv" style="background: url(<?php echo "$foto"; ?>) no-repeat;">
                                <div class="lente" id="diminuir1">

                                </div>

                                <div class="conteudo">
                                    <h3><i class="fa fa-check-circle" aria-hidden="true"></i>&nbsp;<?php echo $conteudoTaxonomia->name;?></h3>


                                    <ul>
                                        <?php
                                            $servicos = new WP_Query(array(
                                                'post_type'         => 'servico',
                                                'posts_per_page'    => -1,
                                                'tax_query'         => array(
                                                                                array(
                                                                                    'taxonomy' => 'categoriaServico',
                                                                                    'field'    => 'slug',
                                                                                    'terms'    => $conteudoTaxonomia->slug
                                                                                )
                                                                            )
                                                )
                                            );
                                            while ( $servicos->have_posts() ) : $servicos->the_post();
                                        ?>
                                        <li><h3>|  &nbsp<?php the_title(); ?></h3></li>

                                        <?php endwhile; wp_reset_query(); ?>

                                    </ul>

                                </div>
                            </div>
                    <?php
                    endforeach;
                ?>

Any suggestions?

    
asked by anonymous 13.07.2017 / 04:08

1 answer

1

You will set the fields outside the function to register the taxonomy, using {taxonomy}_edit_form_fields actions. / or {taxonomy}_add_form_fields

add_action( "categoriaServico_edit_form_fields", array( $this, 'exibe_campo' ), 10, 2 )
add_action( "categoriaServico_add_form_fields", array( $this, 'exibe_campo' ), 10, 2 )

public function exibe_campo( $tag, $taxonomy ) {
    ?>
    <tr class="form-field">
        <th scope="row">
            <label for="campo">Titulo do campo</label>
        </th>
        <td>
            <input type="text" name="campo" value="" />
            <p class="description">Descrição do campo</p>
        </td>
    </tr>
    <?php
}

Do not forget to also create a function to save your fields, WP does not do this by default:

add_action( 'edited_terms', array( $this, 'salvar' ), 10, 2 );

public function salvar( $term_id, $taxonomy ) {
    $valor = $_POST['campo'];
    // valide permissões, nonces e outros parâmetros necessários aqui
    // e sanitize os dados antes de salvar

        update_term_meta( $term_id, 'term_meta_key', $valor );
    }
}
    
13.07.2017 / 05:01