Advertisement
miriamdepaula

Para a Gislaine: Imagem na taxonomia

Oct 21st, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.23 KB | None | 0 0
  1. <?php
  2. //extra field in taxonomy: receita
  3. add_action ('receita_edit_form_fields', 'tax_meta_fields'); //hook {tax-name}_edit_form_fields
  4. add_action('receita_add_form_fields','tax_meta_fields'); //hook {tax-name}_add_form_fields
  5.  
  6. function tax_meta_fields( $tag ) {    //check for existing featured ID
  7.  
  8.     $t_id = $tag->term_id;
  9.     $Tax_meta = get_option("term_$t_id");
  10.  
  11.     echo '<tr class="form-field">';
  12.     echo '  <th scope="row" valign="top"><label for="category_foto">Imagem</label></th>';
  13.     echo '      <td>';
  14.     echo '          <input type="text" name="Tax_meta[tax_image]" id="Tax_meta_tax_image" size="25" style="width:60%;" value="';
  15.     echo ($Tax_meta['tax_image']) ? $Tax_meta['tax_image'] : '';
  16.     echo '">';
  17.     echo '          <input id="upload_file_button" type="button" class="button-primary" value="Enviar Imagem" style="width:100px" />';
  18.     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>';
  19.     echo '      </td>';
  20.     echo '</tr>';
  21.    
  22. }
  23.  
  24. // save extra taxonomy extra fields hook
  25. add_action ('edited_receita', 'save_tax_meta_fileds'); //hook edited_{tax-name}
  26. add_action('created_receita','save_tax_meta_fileds'); //hook created_{tax-name}
  27.  
  28. // save extra taxonomy extra fields callback function
  29. function save_tax_meta_fileds( $term_id ) {
  30.     if ( isset( $_POST['Tax_meta'] ) ) {
  31.         $t_id = $term_id;
  32.         $Tax_meta = get_option( "term_$t_id" );
  33.         $Tax_keys = array_keys($_POST['Tax_meta']);
  34.             foreach ($Tax_keys as $key){
  35.             if (isset($_POST['Tax_meta'][$key])){
  36.                 $Tax_meta[$key] = $_POST['Tax_meta'][$key];
  37.             }
  38.         }
  39.         //save the option array
  40.         update_option( "term_$t_id", $Tax_meta );
  41.     }
  42. }
  43.  
  44. function mediauploader_scripts() {
  45.     wp_enqueue_script('media-upload');
  46.     wp_enqueue_script('thickbox');
  47. }
  48.  
  49. function mediauploader_admin_styles() {
  50.     wp_enqueue_style('thickbox');
  51. }
  52.  
  53. add_action('admin_print_scripts', 'mediauploader_scripts');
  54. add_action('admin_print_styles', 'mediauploader_admin_styles');
  55.  
  56. add_action( 'admin_head', 'metabox_uploader_script' );
  57. function metabox_uploader_script() { ?>
  58.     <script type="text/javascript">
  59.         jQuery(document).ready(function() {
  60.  
  61.             var formfield;
  62.             var header_clicked = false;
  63.  
  64.             jQuery( '#upload_file_button' ).click( function() {
  65.                 formfield = jQuery( '#Tax_meta_tax_image' ).attr( 'name' );
  66.                 tb_show( 'Receita: Imagem', 'media-upload.php?type=image&TB_iframe=true' );
  67.                 header_clicked = true;
  68.  
  69.                 return false;
  70.             });
  71.  
  72.             // Guarda o uploader original
  73.             window.original_send_to_editor = window.send_to_editor;
  74.  
  75.             // Sobrescreve a função nativa e preenche o campo com a URL
  76.             window.send_to_editor = function( html ) {
  77.                 if ( header_clicked ) {
  78.                    
  79.                     fileurl = jQuery( html ).attr( 'href' );
  80.                     imgsrc = jQuery( 'img', html ).attr( 'src' ); //console.log(imgsrc);                   
  81.                    
  82.                     jQuery( '#Tax_meta_tax_image' ).val( imgsrc );
  83.                    
  84.                     header_clicked = false;
  85.                     tb_remove();
  86.                 }
  87.                 else
  88.                 {
  89.                     window.original_send_to_editor( html );
  90.                 }
  91.             }
  92.            
  93.  
  94.         });
  95.        
  96.   </script>
  97. <?php
  98. }
  99. ?>
  100.  
  101. <?php
  102. //COMO PEGAR A IMAGEM NO TEMA: taxonomy-{tax-slug}.php
  103. $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ));
  104. $t_ID = $term->term_id;
  105.  
  106. $term_data = get_option("term_$t_ID");
  107.  
  108. if (isset($term_data['tax_image'])){
  109.     echo '<div class="term_image"><img src="'.$term_data['tax_image'].'" /></div>';
  110. }
  111.  
  112. ?>
  113.  
  114.  
  115. <?php
  116. //PEGAR A IMAGEM EM QUALQUER ARQUIVO DO TEMA
  117. $receitas = get_terms( 'receita', array( 'hide_empty' => 0 ) );
  118. if($receitas){
  119.    
  120.     echo '<ul>';                       
  121.     foreach($receitas as $term){
  122.         $term_data = get_option("term_$term->term_id");
  123.         echo '<li>';
  124.         echo '  <a href="'.get_term_link($term->slug, 'receita').'">';
  125.  
  126.             echo '      <img src="'.$term_data['tax_image'].'" />';
  127.  
  128.         echo '  </a>';
  129.         echo '</li>';
  130.     }
  131.     echo '</ul>';
  132. }
  133. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement