Website Summary Files Commits Branches @commit @diff @stats
path: root / wp-content / themes / carmag / inc / images.php
blob: 6008146fa5b70848ac2964eface39a316615537b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

/**
 * Add custom image sizes attribute to enhance responsive image functionality
 * for post thumbnails
 *
 * @since CarMag 1.0
 *
 * @param array $attr Attributes for the image markup.
 * @param int   $attachment Image attachment ID.
 * @param array $size Registered image size or flat array of height and width dimensions.
 * @return string A source size value for use in a post thumbnail 'sizes' attribute.
 */
function carmag_post_thumbnail_sizes_attr( $attr, $attachment, $size ) {
	if ( 'post-thumbnail' === $size ) {
		  is_active_sidebar( 'sidebar-1' ) && $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 60vw, (max-width: 1362px) 62vw, 840px';
		! is_active_sidebar( 'sidebar-1' ) && $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 88vw, 1200px';
	}
	return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'carmag_post_thumbnail_sizes_attr', 10 , 3 );


/**
 * Prints out better responsive images. Uses the class "responsive-image"
 * 
 * $url The image address
 * $size Name of the thumbnail size
 * $class Custom image class
 * $alt Alternative text if it can't be loaded
 * 
 * @since CarMag v1.1.0
 */
function carmag_featured_image( $size, $class = '', $attr = '' ) {
	if ( ! has_post_thumbnail() ) {
		return;
	}

	if ( empty( $size ) ) {
		return;
	}

	if ( empty( $class ) ) {
		$class = '';
	}

	// Print the HTML of the image
	echo "<picture class='entry-featured-image responsive-image ${class}'>";
	
	// use the standard featured image function
	the_post_thumbnail( $size, $attr );
	
	echo "</picture>";
}

/**
 * Returns better responsive images
 * 
 * @since  CarMag v1.1.0
 */
function carmag_get_responsive_image() {

}