Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //extra field in taxonomy: receita
- add_action ('receita_edit_form_fields', 'tax_meta_fields'); //hook {tax-name}_edit_form_fields
- add_action('receita_add_form_fields','tax_meta_fields'); //hook {tax-name}_add_form_fields
- function tax_meta_fields( $tag ) { //check for existing featured ID
- $t_id = $tag->term_id;
- $Tax_meta = get_option("term_$t_id");
- echo '<tr class="form-field">';
- echo ' <th scope="row" valign="top"><label for="category_foto">Imagem</label></th>';
- echo ' <td>';
- echo ' <input type="text" name="Tax_meta[tax_image]" id="Tax_meta_tax_image" size="25" style="width:60%;" value="';
- echo ($Tax_meta['tax_image']) ? $Tax_meta['tax_image'] : '';
- echo '">';
- echo ' <input id="upload_file_button" type="button" class="button-primary" value="Enviar Imagem" style="width:100px" />';
- echo ' <p><em>Clique no botão <strong>"Enviar Imagem"</strong> para fazer o upload de uma imagem. Ao terminar, clique no botão <strong>"Inserir no post"</strong>.</em></p>';
- echo ' </td>';
- echo '</tr>';
- }
- // save extra taxonomy extra fields hook
- add_action ('edited_receita', 'save_tax_meta_fileds'); //hook edited_{tax-name}
- add_action('created_receita','save_tax_meta_fileds'); //hook created_{tax-name}
- // save extra taxonomy extra fields callback function
- function save_tax_meta_fileds( $term_id ) {
- if ( isset( $_POST['Tax_meta'] ) ) {
- $t_id = $term_id;
- $Tax_meta = get_option( "term_$t_id" );
- $Tax_keys = array_keys($_POST['Tax_meta']);
- foreach ($Tax_keys as $key){
- if (isset($_POST['Tax_meta'][$key])){
- $Tax_meta[$key] = $_POST['Tax_meta'][$key];
- }
- }
- //save the option array
- update_option( "term_$t_id", $Tax_meta );
- }
- }
- function mediauploader_scripts() {
- wp_enqueue_script('media-upload');
- wp_enqueue_script('thickbox');
- }
- function mediauploader_admin_styles() {
- wp_enqueue_style('thickbox');
- }
- add_action('admin_print_scripts', 'mediauploader_scripts');
- add_action('admin_print_styles', 'mediauploader_admin_styles');
- add_action( 'admin_head', 'metabox_uploader_script' );
- function metabox_uploader_script() { ?>
- <script type="text/javascript">
- jQuery(document).ready(function() {
- var formfield;
- var header_clicked = false;
- jQuery( '#upload_file_button' ).click( function() {
- formfield = jQuery( '#Tax_meta_tax_image' ).attr( 'name' );
- tb_show( 'Receita: Imagem', 'media-upload.php?type=image&TB_iframe=true' );
- header_clicked = true;
- return false;
- });
- // Guarda o uploader original
- window.original_send_to_editor = window.send_to_editor;
- // Sobrescreve a função nativa e preenche o campo com a URL
- window.send_to_editor = function( html ) {
- if ( header_clicked ) {
- fileurl = jQuery( html ).attr( 'href' );
- imgsrc = jQuery( 'img', html ).attr( 'src' ); //console.log(imgsrc);
- jQuery( '#Tax_meta_tax_image' ).val( imgsrc );
- header_clicked = false;
- tb_remove();
- }
- else
- {
- window.original_send_to_editor( html );
- }
- }
- });
- </script>
- <?php
- }
- ?>
- <?php
- //COMO PEGAR A IMAGEM NO TEMA: taxonomy-{tax-slug}.php
- $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ));
- $t_ID = $term->term_id;
- $term_data = get_option("term_$t_ID");
- if (isset($term_data['tax_image'])){
- echo '<div class="term_image"><img src="'.$term_data['tax_image'].'" /></div>';
- }
- ?>
- <?php
- //PEGAR A IMAGEM EM QUALQUER ARQUIVO DO TEMA
- $receitas = get_terms( 'receita', array( 'hide_empty' => 0 ) );
- if($receitas){
- echo '<ul>';
- foreach($receitas as $term){
- $term_data = get_option("term_$term->term_id");
- echo '<li>';
- echo ' <a href="'.get_term_link($term->slug, 'receita').'">';
- echo ' <img src="'.$term_data['tax_image'].'" />';
- echo ' </a>';
- echo '</li>';
- }
- echo '</ul>';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement