daniofilho
8/26/2016 - 1:07 PM

Pegar todas as imagens anexadas a página.php

<?php 

					global $post;
					
					$images = get_posts(array("post_type" => "attachment", 
											  "showposts" => -1, 
											  "post_parent" => $post->ID, 
											  "orderby" => "menu_order")
									    );
									    
					//Pega as imagens do post
					$imagensNoPost = getImagensPost();
					
					foreach($images as $image) {
					
						 	$alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true);
							$image_title = $image->post_title;
							$link = $image->post_excerpt;
							$description = $image->post_content;
							
							if ( $link == "" ){ $link = "#"; }
							
							$urls = array ();
					  
						list ($url, $width, $height) = wp_get_attachment_image_src($image->ID, $s);
						
							//Se não estiver no array, imprime a imagem
							if (! in_array($url, $imagensNoPost) ) {
								
								$url = resizeImg( $url, 1920, 400 );
							?>
							
									<li>
									
										<a href="<?php echo $description; ?>">
										
											<img src="<?php echo $url; ?>" alt="<?php echo $image_title; ?>" />
											
										</a>
									
									</li>
							
							<?php
							
							}//if  
				
					}
			
//Cria um array com todas as imagens que estão no conteúdo
function getImagensPost() {

		$arrayImg = array();

		global $post;

		$szPostContent = $post->post_content;
		$szSearchPattern = '~<img [^\>]*\ />~';

		// Run preg_match_all to grab all the images and save the results in $aPics
		preg_match_all( $szSearchPattern, $szPostContent, $aPics );

		// Check to see if we have at least 1 image
		$iNumberOfPics = count($aPics[0]);

		if ( $iNumberOfPics > 0 ) {
		     // Now here you would do whatever you need to do with the images
		     // For this example the images are just displayed
		     for ( $i=0; $i < $iNumberOfPics ; $i++ ) {

		            $imgPost =  $aPics[0][$i];

					// get the src for that image
					$pattern = '/src="([^"]*)"/';
					preg_match($pattern, $imgPost, $matches);
					$src = $matches[1];
					unset($matches);
					
					//OU
					//$xpath = new DOMXPath(@DOMDocument::loadHTML($imgPost));
					//$src = $xpath->evaluate("string(//img/@src)");

					//Remove os tamanhos do WP
					$pattern = array ( '/-(\d{1,5})x(\d{1,5})/' ); // encontra tudo dentro de -ZZZxZZZ onde a quantidade de Z pode ser 1 ou 5 e Z é um número

					$src = preg_replace($pattern, "", $src);
					
					//Adiciona o link da imagem no array
					array_push($arrayImg, $src);

		     };
		};

		//Devolve o array
		return $arrayImg;
?>