Changeset 1939482
- Timestamp:
- 09/11/2018 06:52:18 PM (8 years ago)
- Location:
- categories-multiple-images/trunk
- Files:
-
- 2 edited
-
categories-multiple-images.php (modified) (10 diffs)
-
readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
categories-multiple-images/trunk/categories-multiple-images.php
r1789918 r1939482 4 4 * Plugin Name: Categories Multiple Images 5 5 * Description: Categories Multiple Images Plugin allows you to add multiple images to categories or any other custom taxonomy. 6 * Version: 1. 0.36 * Version: 1.1 7 7 * Author: Lior Broshi, Binternet 8 8 * Author URI: http://www.binternet.co.il … … 11 11 */ 12 12 13 /* Copyright 201 5Lior Broshi (binternet) (email : lior@binternet.co.il)13 /* Copyright 2018 Lior Broshi (binternet) (email : lior@binternet.co.il) 14 14 15 15 This program is free software; you can redistribute it and/or modify … … 179 179 } 180 180 181 182 181 /** 183 182 * Categories_Multiple_Images::options_page() … … 313 312 314 313 } 315 314 315 /* 316 |-------------------------------------------------------------------------- 317 | Statics 318 |-------------------------------------------------------------------------- 319 */ 320 316 321 /** 317 322 * Categories_Multiple_Images::get_image() 318 323 * Returns the image(s) 319 324 * @param mixed $term_id Term ID 320 * @param mixed $image_number Specific Image Number 325 * @param mixed $image_number Specific Image Number -OR- 'random' which returns a random image 321 326 * @param string $size Image Size [thumb, full etc...] 322 * @param bool $return_placeholder Use image placeholder in case no image was found327 * @param bool $return_placeholder Use image placeholder in case no image was found 323 328 * @return 324 329 */ … … 330 335 331 336 if ( empty( $term_id ) ) { 332 337 # No specific term ID was provided, try fetching from WP_Query 333 338 if ( is_category() ) { 334 339 $term_id = get_query_var('cat'); … … 343 348 return FALSE; 344 349 } 345 346 350 } 347 351 352 # Placeholder image 348 353 $placeholder_image = self::get_placeholder_image(); 349 354 350 355 # Load the image(s) 351 356 if ( empty( $image_number ) ) { 352 353 # Get them all 354 $total_images = self::validate( get_option( 'cmi_total_images' ), 'total_images' ); 355 $images = array(); 356 357 for ( $i = 1, $n = $total_images; $i <= $n; $i++ ) { 358 359 # Get that image 360 $image = get_option( "cmi_taxonomy_image{$i}_{$term_id}" ); 361 362 if ( empty( $image ) ) { 363 364 # Wops, no such image, placeholder? 365 $image = ( TRUE == $return_placeholder ) ? $placeholder_image : $image; 366 367 } 368 elseif ( ! empty( $size ) ) { 369 370 # Get specific size 371 $thumb_id = self::get_attachment_id_from_url( $image ); 372 373 if ( ! empty( $thumb_id ) ) { 374 # Gotcha 375 $attachment = wp_get_attachment_image_src( $thumb_id, $size ); 376 $image = $attachment[0]; 377 } 378 379 } 380 381 # Add to result array 382 $images[] = $image; 383 } 384 385 return $images; 357 # No specific image was given, return them all 358 return self::get_all_images( $term_id, $return_placeholder ); 359 } 360 elseif ( strtolower( $image_number ) == 'rand' OR strtolower( $image_number ) == 'random' ) { 361 # A Random image was requested 362 $all_images = self::get_all_images( $term_id, $return_placeholder ); 363 return $all_images[ array_rand( $all_images ) ]; 386 364 } 387 365 else { 388 389 # Get specific image 390 366 # Get a specific image 391 367 $image = get_option( "cmi_taxonomy_image{$image_number}_{$term_id}" ); 392 368 393 369 if ( empty( $image ) ) { 394 395 370 # Wops, no such image, placeholder? 396 371 $image = ( TRUE == $return_placeholder ) ? $placeholder_image : $image; 397 398 372 } 399 373 elseif ( ! empty( $size ) ) { 400 401 374 # Get specific size 402 375 $thumb_id = self::get_attachment_id_from_url( $image ); 403 404 376 if ( ! empty( $thumb_id ) ) { 405 377 # Gotcha … … 407 379 $image = $attachment[0]; 408 380 } 409 410 381 } 411 382 412 383 return $image; 413 384 } 414 415 385 } 416 386 … … 423 393 /** 424 394 * Categories_Multiple_Images::validate() 425 * 395 * Validation of misc stuff such as array, 'total_images' value 426 396 * @param mixed $value 427 397 * @param mixed $what … … 431 401 432 402 if ( empty( $what ) ) { 433 return ;403 return NULL; 434 404 } 435 405 … … 459 429 460 430 } 431 432 /** 433 * Returns all images for a given term ID 434 * @param integer $term_id 435 * @return array 436 */ 437 static function get_all_images( $term_id, $return_placeholder = TRUE ) { 438 439 $placeholder_image = self::get_placeholder_image(); 440 $total_images = self::validate( get_option( 'cmi_total_images' ), 'total_images' ); 441 $images = array(); 442 443 for ( $i = 1, $n = $total_images; $i <= $n; $i++ ) { 444 445 # Get that image 446 $image = get_option( "cmi_taxonomy_image{$i}_{$term_id}" ); 447 448 if ( empty( $image ) ) { 449 # Wops, no such image, placeholder? 450 if ( TRUE == $return_placeholder ) { 451 $image = $placeholder_image; 452 } 453 else { 454 # Skip this image since its empty and no placeholder should be used 455 continue; 456 } 457 } 458 elseif ( ! empty( $size ) ) { 459 460 # Get specific size 461 $thumb_id = self::get_attachment_id_from_url( $image ); 462 463 if ( ! empty( $thumb_id ) ) { 464 # Gotcha 465 $attachment = wp_get_attachment_image_src( $thumb_id, $size ); 466 $image = $attachment[0]; 467 } 468 469 } 470 471 # Add to result array 472 $images[] = $image; 473 } 474 475 # Return all images 476 return $images; 477 } 461 478 462 479 } -
categories-multiple-images/trunk/readme.txt
r1789921 r1939482 33 33 2. Activate the plugin through the 'Plugins' menu in WordPress 34 34 3. Place `<?php Categories_Multiple_Images::get_image( term_id, image_number, image_size, use_placeholder ); ?> ` in your templates 35 Values are: 36 - term_id - Category ID (or any other term_id) 37 - image_number - Desired image number (starting from 1), you can also use 'random' and a random image from that category will be returned 38 - image_size - desired image size, defaults to 'full' 39 - use_placeholder - should a placeholder should be returned if the image does not exists 35 40 36 41 == Changelog == 42 43 = 1.1 = 44 * Added a new option to pass 'random' as an image number and get a random image for a given category ID 45 * Some documentation and refactoring 37 46 38 47 = 1.0.3 =
Note: See TracChangeset
for help on using the changeset viewer.