<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Images - Themespress</title>
	<atom:link href="https://themespress.ca/sujets/images/feed/" rel="self" type="application/rss+xml" />
	<link>https://themespress.ca</link>
	<description>Plugins, tutoriels et ressources WordPress</description>
	<lastBuildDate>Mon, 17 Jan 2022 20:35:15 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>

<image>
	<url>https://themespress.ca/wp-content/uploads/2020/10/cropped-Favicon-Themepress.ca_-32x32.png</url>
	<title>Images - Themespress</title>
	<link>https://themespress.ca</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Afficher les images attachées Post ID WordPress</title>
		<link>https://themespress.ca/afficher-images-attachees-post-id-wordpress/</link>
					<comments>https://themespress.ca/afficher-images-attachees-post-id-wordpress/#respond</comments>
		
		<dc:creator><![CDATA[Themespress]]></dc:creator>
		<pubDate>Sat, 28 Jan 2017 16:53:26 +0000</pubDate>
				<category><![CDATA[Tutoriels]]></category>
		<category><![CDATA[Images]]></category>
		<guid isPermaLink="false">https://themespress.ca/?p=1476</guid>

					<description><![CDATA[]]></description>
										<content:encoded><![CDATA[<div class="wpb-content-wrapper"><div class="vc_row wpb_row vc_row-fluid"><div class="wpb_column vc_column_container vc_col-sm-12"><div class="vc_column-inner"><div class="wpb_wrapper">
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p><strong>Afficher les images attachées Post ID WordPress :</strong> Nous allons dans ce tutoriel se construire une fonction requête qui va permettre d’afficher les images attachées au Post ID.</p>
<p>Imaginons par un formulaire, vous insérez un article et que vous téléchargez plusieurs images, ces images téléchargées deviennent en objet get_attachment_image.</p>
<p>Et c’est dans cette fonction que nous allons effectuer la requête allant chercher les get_attachment_image du POST ID. Nous souhaitons aussi dans notre requête afficher les images en format $size = ‘medium’;</p>
<p>Voici les tailles images WordPress par défaut : ‘thumbnail’, ‘medium’, ‘large’, ‘full’. Ou utiliser des thumbnails images que vous avez personnalisées.</p>
<p>On dit aussi dans notre fonction que s’il n’y a pas d’images attachées au POST ID, n’affiche rien. Donc on prépare nos arguments et on n’affiche rien si vide.</p>
<p>Ensuite nous bouclons nos résultats en foreach avec comme vous le constatez du html personnalisé qui pourra être changé par le vôtre.</p>

		</div>
	</div>
<div id="ts-enlighterjs-container-2345590" class="ts-enlighterjs-container-single-enabled  " style="width: 100%;  margin-top: 0px; margin-bottom: 0px;" data-enlighter-doubleclick="true" data-enlighter-windowbutton="true" data-enlighter-windowtext="New Window" data-enlighter-rawbutton="true" data-enlighter-rawtext="RAW Code" data-enlighter-infobutton="false" data-enlighter-infotext="EnlighterJS" data-enlighter-indent="2"><pre id="ts-enlighterjs-pre-2345590" class="" style="white-space: pre-wrap; height: 100%; margin: 0; padding: 0;" data-enlighter-language="php" data-enlighter-theme="enlighter" data-enlighter-group="" data-enlighter-title="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="1" data-enlighter-highlight="">function via_get_images_attachees() {
	global $post;
    $thumb_id = get_post_thumbnail_id($post-&gt;ID);
	$id = intval( $post-&gt;ID );
	$size = &#039;medium&#039;;
	$attachments = get_children( array(
			&#039;post_parent&#039;     =&gt; $id,
			&#039;post_status&#039;     =&gt; &#039;inherit&#039;,
			&#039;post_type&#039;       =&gt; &#039;attachment&#039;,
			&#039;post_mime_type&#039;  =&gt; &#039;image&#039;,
			&#039;order&#039;           =&gt; &#039;ASC&#039;,
			&#039;orderby&#039;         =&gt; &#039;menu_order&#039;,
            &#039;post__not_in&#039;    =&gt; array( $thumb_id )
		) );
	if ( empty( $attachments ) )
	return &#039;&#039;;
	$output = &quot;\n&quot;;
	$output .= &#039;&lt;div id=&quot;grid&quot;&gt;&#039;; 
    
	/*** Loop through each attachment ***/ 
	foreach ( $attachments as $id =&gt; $attachment ) : $img_cap = $attachment-&gt;post_excerpt; 
		// New line to get the caption 
		$img_alt = get_post_meta($attachment_id, &#039;_wp_attachment_image_alt&#039;, true); 
		//alt 
		if ($img_alt == &#039;&#039;) : 
		$img_alt = $attachment-&amp;gt;post_title; endif; $title = esc_html( $attachment-&amp;gt;post_title, 1 ); 
		$img = wp_get_attachment_image_src( $id, $size ); 
		$output .= &#039;&lt;div class=&quot;box&quot;&gt;&#039;; 
		$output .= &#039;&lt;div data-thumbnail=&quot;&#039; . esc_url( wp_get_attachment_url( $id ) ) . &#039;&quot;&gt;&lt;/div&gt;&#039;; 
		$output .= &#039;&lt;div data-image=&quot;&#039; . esc_url( wp_get_attachment_url( $id ) ) . &#039;&quot;&gt;&lt;/div&gt;&#039;; 
		$output .= &#039;&lt;div class=&quot;thumbnail-caption&quot;&gt;&#039;; 
		$output .= &#039;&lt;h3&gt;&#039;. $img_alt .&#039;&lt;/h3&gt;&#039;; 
		$output .= &#039;&lt;/div&gt;&#039;; 
		$output .= &#039;&lt;div class=&quot;lightbox-text&quot;&gt;&#039;; 
		$output .= &#039;&quot; &#039;. $img_alt .&#039; &quot; &#039;; 
		$output .= &#039;&lt;/div&gt;&#039;; 
		$output .= &#039;&lt;/div&gt;&#039;; 
	endforeach; 
	$output .= &#039;&lt;/div&gt;&#039;; 
	
	return $output; 
}</pre></div>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>Remarquez aussi que dans la boucle nous voulons comme texte le alt de l’image qui est stocké dans une variable <strong>$img_alt</strong>. Bonne ou mauvaise idée, à vous de juger !</p>
<p>Maintenant prenons l’occasion d’afficher les résultats dans un template :</p>

		</div>
	</div>
<div id="ts-enlighterjs-container-3067186" class="ts-enlighterjs-container-single-enabled  " style="width: 100%;  margin-top: 0px; margin-bottom: 0px;" data-enlighter-doubleclick="true" data-enlighter-windowbutton="true" data-enlighter-windowtext="New Window" data-enlighter-rawbutton="true" data-enlighter-rawtext="RAW Code" data-enlighter-infobutton="false" data-enlighter-infotext="EnlighterJS" data-enlighter-indent="2"><pre id="ts-enlighterjs-pre-3067186" class="" style="white-space: pre-wrap; height: 100%; margin: 0; padding: 0;" data-enlighter-language="standard" data-enlighter-theme="enlighter" data-enlighter-group="" data-enlighter-title="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="1" data-enlighter-highlight="">&lt;?php echo via_get_images_attachees(); ?&gt;</pre></div>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>On sait pertinemment que le Post Thumbnail va être considéré comme une image attachée. Donc le Post Thumbnail va se retrouver dans la liste d’images. On peut dans nos arguments, mettre une exclusion du Post Thumbnail dans une variable <strong>$thumb_id</strong>.</p>
<p>Voici un exemple d’exclusion du Post Thumbnail : Donnons l’argument suivant après le global $post;</p>

		</div>
	</div>
<div id="ts-enlighterjs-container-8790632" class="ts-enlighterjs-container-single-enabled  " style="width: 100%;  margin-top: 0px; margin-bottom: 0px;" data-enlighter-doubleclick="true" data-enlighter-windowbutton="true" data-enlighter-windowtext="New Window" data-enlighter-rawbutton="true" data-enlighter-rawtext="RAW Code" data-enlighter-infobutton="false" data-enlighter-infotext="EnlighterJS" data-enlighter-indent="2"><pre id="ts-enlighterjs-pre-8790632" class="" style="white-space: pre-wrap; height: 100%; margin: 0; padding: 0;" data-enlighter-language="standard" data-enlighter-theme="enlighter" data-enlighter-group="" data-enlighter-title="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="1" data-enlighter-highlight="">$thumb_id = get_post_thumbnail_id($post-&gt;ID);</pre></div>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>Ensuite, vous ajouter l’argument exclusion dans la variable $attachments en déclarant un post__not_in de la variable <strong>$thumb_id</strong>.</p>

		</div>
	</div>
<div id="ts-enlighterjs-container-7363593" class="ts-enlighterjs-container-single-enabled  " style="width: 100%;  margin-top: 0px; margin-bottom: 0px;" data-enlighter-doubleclick="true" data-enlighter-windowbutton="true" data-enlighter-windowtext="New Window" data-enlighter-rawbutton="true" data-enlighter-rawtext="RAW Code" data-enlighter-infobutton="false" data-enlighter-infotext="EnlighterJS" data-enlighter-indent="2"><pre id="ts-enlighterjs-pre-7363593" class="" style="white-space: pre-wrap; height: 100%; margin: 0; padding: 0;" data-enlighter-language="standard" data-enlighter-theme="enlighter" data-enlighter-group="" data-enlighter-title="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="1" data-enlighter-highlight="">$attachments = get_children( array(
	&#039;post_parent&#039;    =&gt; $id,
	&#039;post_status&#039;    =&gt; &#039;inherit&#039;,
	&#039;post_type&#039;      =&gt; &#039;attachment&#039;,
	&#039;post_mime_type&#039; =&gt; &#039;image&#039;,
	&#039;order&#039;          =&gt; &#039;ASC&#039;,
	&#039;orderby&#039;        =&gt; &#039;menu_order&#039;,
	&#039;post__not_in&#039;   =&gt; array( $thumb_id )
) );</pre></div><h2 style="text-align: left" class="vc_custom_heading vc_do_custom_heading" >Le code final pour afficher les images attachées Post ID WordPress en excluant le Post Thumbnail</h2><div id="ts-enlighterjs-container-4687305" class="ts-enlighterjs-container-single-enabled  " style="width: 100%;  margin-top: 0px; margin-bottom: 0px;" data-enlighter-doubleclick="true" data-enlighter-windowbutton="true" data-enlighter-windowtext="New Window" data-enlighter-rawbutton="true" data-enlighter-rawtext="RAW Code" data-enlighter-infobutton="false" data-enlighter-infotext="EnlighterJS" data-enlighter-indent="2"><pre id="ts-enlighterjs-pre-4687305" class="" style="white-space: pre-wrap; height: 100%; margin: 0; padding: 0;" data-enlighter-language="php" data-enlighter-theme="enlighter" data-enlighter-group="" data-enlighter-title="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="1" data-enlighter-highlight="">function via_get_images_attachees() {
	global $post;
    $thumb_id = get_post_thumbnail_id($post-&gt;ID);
	$id = intval( $post-&gt;ID );
	$size = &#039;medium&#039;;
	$attachments = get_children( array(
			&#039;post_parent&#039;     =&gt; $id,
			&#039;post_status&#039;     =&gt; &#039;inherit&#039;,
			&#039;post_type&#039;       =&gt; &#039;attachment&#039;,
			&#039;post_mime_type&#039;  =&gt; &#039;image&#039;,
			&#039;order&#039;           =&gt; &#039;ASC&#039;,
			&#039;orderby&#039;         =&gt; &#039;menu_order&#039;,
            &#039;post__not_in&#039;    =&gt; array( $thumb_id )
		) );
	if ( empty( $attachments ) )
	return &#039;&#039;;
	$output = &quot;\n&quot;;
	$output .= &#039;&lt;div id=&quot;grid&quot;&gt;&#039;; 
    
	/*** Loop through each attachment ***/ 
	foreach ( $attachments as $id =&gt; $attachment ) : $img_cap = $attachment-&gt;post_excerpt; 
		// New line to get the caption 
		$img_alt = get_post_meta($attachment_id, &#039;_wp_attachment_image_alt&#039;, true); 
		//alt 
		if ($img_alt == &#039;&#039;) : 
		$img_alt = $attachment-&amp;gt;post_title; endif; $title = esc_html( $attachment-&amp;gt;post_title, 1 ); 
		$img = wp_get_attachment_image_src( $id, $size ); 
		$output .= &#039;&lt;div class=&quot;box&quot;&gt;&#039;; 
		$output .= &#039;&lt;div data-thumbnail=&quot;&#039; . esc_url( wp_get_attachment_url( $id ) ) . &#039;&quot;&gt;&lt;/div&gt;&#039;; 
		$output .= &#039;&lt;div data-image=&quot;&#039; . esc_url( wp_get_attachment_url( $id ) ) . &#039;&quot;&gt;&lt;/div&gt;&#039;; 
		$output .= &#039;&lt;div class=&quot;thumbnail-caption&quot;&gt;&#039;; 
		$output .= &#039;&lt;h3&gt;&#039;. $img_alt .&#039;&lt;/h3&gt;&#039;; 
		$output .= &#039;&lt;/div&gt;&#039;; 
		$output .= &#039;&lt;div class=&quot;lightbox-text&quot;&gt;&#039;; 
		$output .= &#039;&quot; &#039;. $img_alt .&#039; &quot; &#039;; 
		$output .= &#039;&lt;/div&gt;&#039;; 
		$output .= &#039;&lt;/div&gt;&#039;; 
	endforeach; 
	$output .= &#039;&lt;/div&gt;&#039;; 
	
	return $output; 
}</pre></div></div></div></div></div>
</div><p>The post <a href="https://themespress.ca/afficher-images-attachees-post-id-wordpress/">Afficher les images attachées Post ID WordPress</a> first appeared on <a href="https://themespress.ca">Themespress</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://themespress.ca/afficher-images-attachees-post-id-wordpress/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
