Advertisement
miriamdepaula

WordPress: Anexar Images através de um metabox

Feb 5th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.25 KB | None | 0 0
  1. <?php
  2. /*
  3. Aqui vai um snippet para facilmente anexar imagens a posts com um simples METABOX de seleção de miniaturas. Adicione o primeiro trecho à functions.php do seu tema wordress. Coloque o segundo trecho dentro do loop de seu tema wordpress para exibir as imagens em seus posts ou páginas.
  4. Achei isso muito mais fácil para os clientes, para selecionar as imagens sem entrar na biblioteca e anexar imagens a posts.
  5. Snippet tirado de: http://wpsnipp.com/index.php/functions-php/attach-images-with-simple-thumbnail-selection-metabox/ (todos creditos ao autor do código)
  6. */
  7. add_action("admin_init", "images_init");
  8. add_action('save_post', 'save_images_link');
  9. function images_init(){
  10.           $post_types = get_post_types();
  11.           foreach ( $post_types as $post_type ) {
  12.         add_meta_box("my-images", "Pictures", "images_link", $post_type, "normal", "low");
  13.       }
  14.     }
  15. function images_link(){
  16.     global $post;
  17.     $custom  = get_post_custom($post->ID);
  18.     $link    = $custom["_link"][0];
  19.     $count   = 0;
  20.     echo '<div class="link_header">';
  21.     $query_images_args = array(
  22.         'post_type' => 'attachment',
  23.         'post_mime_type' =>array(
  24.                         'jpg|jpeg|jpe' => 'image/jpeg',
  25.                         'gif' => 'image/gif',
  26.                 'png' => 'image/png',
  27.                 ),
  28.         'post_status' => 'inherit',
  29.         'posts_per_page' => -1,
  30.         );
  31.     $query_images = new WP_Query( $query_images_args );
  32.     $images = array();
  33.     echo '<div class="frame">';
  34.     $thelinks = explode(',', $link);
  35.     foreach ( $query_images->posts as $file) {
  36.        if(in_array($images[]= $file->ID, $thelinks)){
  37.           echo '<label><input type="checkbox" group="images" value="'.$images[]= $file->ID.'" checked /><img src="'.$images[]= $file->guid.'" width="60" height="60" /></label>';
  38.          }else{
  39.           echo '<label><input type="checkbox" group="images" value="'.$images[]= $file->ID.'" /><img src="'.$images[]= $file->guid.'" width="60" height="60" /></label>';
  40.          }
  41.         $count++;
  42.     }
  43.     echo '<br /><br /></div></div>';
  44.     echo '<input type="hidden" name="link" class="field" value="'.$link.'" />';
  45.     echo '<div class="images_count"><span>Files: <b>'.$count.'</b></span> <div class="count-selected"></div></div>';
  46. }
  47. function save_images_link(){
  48.     global $post;
  49.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ return $post->ID; }
  50.     update_post_meta($post->ID, "_link", $_POST["link"]);
  51. }
  52. add_action( 'admin_head-post.php', 'images_css' );
  53. add_action( 'admin_head-post-new.php', 'images_css' );
  54. function images_css() {
  55.     echo '<style type="text/css">
  56.        #my-images .inside{padding:0px !important;margin:0px !important;}
  57.     .frame{
  58.         width:100%;
  59.         height:320px;
  60.         overflow:auto;
  61.                background:#e5e5e5;
  62.         padding-bottom:10px;
  63.         }
  64.     .field{width:800px;}
  65.     #results {
  66.         width:100%;
  67.         overflow:auto;
  68.                background:#e5e5e5;
  69.         padding:0px 0px 10px 0px;
  70.         margin:0px 0px 0px 0px;
  71.         }
  72.     #results img{
  73.         border:solid 5px #FDD153;
  74.         -moz-border-radius:3px;
  75.         margin:10px 0px 0px 10px;
  76.         }
  77.     .frame label{
  78.         margin:10px 0px 0px 10px;
  79.         padding:5px;
  80.         background:#fff;
  81.         -moz-border-radius:3px;
  82.         border:solid 1px #B5B5B5;
  83.         height:60px;
  84.         display:block;
  85.         float:left;
  86.         overflow:hidden;
  87.         }
  88.     .frame label:hover{
  89.         background:#74D3F2;
  90.         }
  91.     .frame label.checked{background:#FDD153 !important;}
  92.     .frame label input{
  93.         opacity:0.0;
  94.         position:absolute;
  95.         top:-20px;
  96.         }
  97.     .images_count{
  98.         font-size:10px;
  99.         color:#666;
  100.         text-transform:uppercase;
  101.         background:#f3f3f3;
  102.         border-top:solid 1px #ccc;
  103.         position:relative;
  104.         }
  105.     .selected_title{border-top:solid 1px #ccc;}
  106.     .images_count span{
  107.         color:#666;
  108.         padding:10px 6px 6px 12px;
  109.         display:block;
  110.         }
  111.     .count-selected{
  112.         font-size:9px;
  113.         font-weight:bold;
  114.         text-transform:normal;
  115.         position:absolute;
  116.         top:10px;
  117.         right:10px;
  118.         }
  119.         </style>';
  120. }
  121. add_action( 'admin_head-post.php', 'images_js' );
  122. add_action( 'admin_head-post-new.php', 'images_js' );
  123. function images_js(){?>
  124. <script type="text/javascript">
  125. jQuery(document).ready(function($){
  126.   $('.frame input').change(function() {
  127.     var values = new Array();
  128.         $("#results").empty();
  129.     var result = new Array();
  130.     $.each($(".frame input:checked"), function() {
  131.         result.push($(this).attr("value"));
  132.         $(this).parent().addClass('checked');
  133.     });
  134.     $('.field').val(result.join(','));
  135.     $('.count-selected').text('Selected: '+result.length);
  136.     $.each($(".frame input:not(:checked)"), function() {
  137.         $(this).parent().removeClass('checked');
  138.     });
  139.   });
  140.     var result = new Array();
  141.     $.each($(".frame input:checked"), function() {
  142.         result.push($(this).attr("value"));
  143.         $(this).parent().addClass('checked');
  144.     });
  145.     $('.field').val(result.join(','));
  146.     $('.count-selected').text('Selected: '+result.length);
  147.     $.each($(".frame input:not(:checked)"), function() {
  148.         $(this).parent().removeClass('checked');
  149.     });
  150. });
  151. </script>
  152. <?}
  153. function wps_thumbnails_list(){
  154.        global $post;
  155.        $image = get_post_meta($post->ID, '_link', true);
  156.        $image = explode(",", $image);
  157.        foreach($image as $images){
  158.         $url = wp_get_attachment_image_src($images, 1, 1);
  159.             echo '<a href="';
  160.         echo $url[0];
  161.         echo '" class="lightbox">';
  162.         echo wp_get_attachment_image($images,'thumbnail', 1, 1);
  163.         echo '</a>';
  164.         }
  165. }
  166.  
  167. ?>
  168.  
  169. <?php
  170. //add this in the loop to list selected images
  171.       wps_thumbnails_list()
  172. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement