- WordPress :5.3
- Statut : non résolu
- Ce sujet contient 16 réponses, 3 participants et a été mis à jour pour la dernière fois par mumu51, le il y a 4 années et 8 mois.
-
AuteurMessages
-
9 avril 2020 à 15 h 58 min #2337825
Bonjour,
Ma configuration WP actuelle
- Version de PHP/MySQL : 7.2.18
- Thème utilisé : Je le créée moi-même
- Extensions en place : Askimet
- Nom de l’hébergeur : En local pour le moment
- Adresse du site : En local
Problème(s) rencontré(s) :
J’ai élaboré un custom-post de type portfolio, pour qu’un traiteur puisse afficher sur son site, toutes photos des banquets auxquels il a participé (un classique).
Je rencontre un problème lorsque je veux afficher une galerie sur la page single-banquets (« banquets » est mon custom type) car certaines des images sont répétées 2 fois, et je ne comprends pas pourquoi, puisque le bout de code suivant est tout de même un snippet que l’on retrouve un peu partout :
function sylvietraiteur_list_attachments() {
$gallery_items = get_children(
array(
'post_parent'=> get_the_ID(),
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => -1,
'order' => 'DESC',
'exclude' => get_post_thumbnail_id()
)
);
if ( $gallery_items ) {
foreach ( $gallery_items as $attachment ) {
echo '<a href="'.wp_get_attachment_url( $attachment->ID ).'" rel="lightbox">';
echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="cls-banquet-item w-25"></a>';
}
}
wp_reset_query();
}Concernant ma page single-banquets.php, celle-ci se découpe en plusieurs morceaux :
get_header();
get_template_part('parts/header/single','header');
while ( have_posts() ) : the_post();
get_template_part('parts/content/single','body');
endwhile;
get_footer();Voici le contenu du body :
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="container d-flex p-5">
<div class="cls-services w-50">
<h4>Services</h4>
<div class="d-flex flex-wrap"><?php sylvietraiteur_get_services_article(); ?></div>
</div>
<div class="cls-description-banquet d-flex justify-content-end">
<p class="mb-0">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
</p>
</div>
</div>
<div class="main single">
<div class="album py-5 bg-light">
<div class="container">
<?php
sylvietraiteur_list_attachments();
?>
</div>
</div>
</div>
</article>La fonction sylvietraiteur_list_attachments() est appelée depuis le fichier functions.php et il s’agit du bout de code que l’on retrouve en premier, plus haut dans ce billet.
Je fais comme cela, car j’utilise les mêmes fonctions un peu partout sur mon thème, et la maintenance en devient facilitée.Si quelqu’un pourrait m’éviter de tourner en rond pour un problème qui semble – certainement et encore ; très petit, je le remercie d’avance !
Dans l’attente, bonne journée à tous et à toutes !9 avril 2020 à 18 h 20 min #2337859Personne ? :/
9 avril 2020 à 18 h 24 min #2337863Bonjour.
Personne ? :/
Si, moi, pour te répondre en deux heures, il fallait déjà être codeur, être disponible, apparemment il n’y en avait pas sur le forum, nous ne sommes que des bénévoles.
🙂
9 avril 2020 à 18 h 38 min #2337870Super !
Avez-vous donc une idée ou une piste pour mon souci ? :/9 avril 2020 à 18 h 41 min #2337872Et non, je ne suis pas codeur, je t’ai juste répondu parce qu’au bout de deux heures, tu t’impatiente, alors je t’explique la raison de l’absence de réponses.
🙂
- Cette réponse a été modifiée le il y a 4 années et 9 mois par PhiLyon.
9 avril 2020 à 18 h 45 min #2337876Ah mince !
Oui effectivement c’est hyper urgent :/
Et je tourne sans trouver de réponses…10 avril 2020 à 1 h 37 min #2337923Bonjour @mumu51,
Ce code n’est pas testé donc vérifier les éventuelles erreurs de syntaxe car je l’ai rédigé rapidos, mais il devrait faire l’affaire.
C’est un simple dédoublonnage inséré dans le foreach.
if ( $gallery_items ) {
$existing_items = array(); // Je crée un tableau pour stocker les ID des attachments déjà récupérés
foreach ( $gallery_items as $attachment ) {
// Je n'affiche l'image que si elle n'existe pas dans le tableau des attachments déjà récupérés
if ( ! in_array( attachment->ID, $existing_items ) ) {
echo '<a href="' . wp_get_attachment_url( $attachment->ID ) . '" rel="lightbox">';
echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="cls-banquet-item w-25"></a>';
array_push( $existing_items, attachment->ID ); // Je stocke l'ID de l'attachment affiché dans le tableau des attachments déjà récupéré
}
}
}10 avril 2020 à 9 h 30 min #2337941Bonjour,
Tout d’abord, merci beaucoup.Niveau syntaxe, il manquait juste des « $ » devant la variable $attachement.
Par contre, cela ne change rien, j’ai toujours mes photos en double… Qu’est-ce que WordPress peut être têtu des fois T_T10 avril 2020 à 9 h 36 min #2337942Ce que je ne comprend pas (que j’ai omis d’expliquer d’ailleurs), c’est que lorsque j’édite mon custom post, et que je le compare avec l’existant, non seulement, je vois des images qui n’apparaissent même pas dans ma galerie (j’ai certainement dû les supprimer à un moment donné) mais en plus en doublon…
11 avril 2020 à 15 h 30 min #2338115Je me permet un petit up 🙂
11 avril 2020 à 16 h 17 min #2338125Re,
Peux tu faire un
var_export( $gallery_items );
juste avant leif
et partager le résultat ici s’il te plaît ?11 avril 2020 à 17 h 54 min #2338135Re,
Bien sûr !
Ci-après, le résultat de ce que j’ai obtenu, avec le code suivant :function sylvietraiteur_list_attachments() {
$gallery_items = get_children(
array(
'post_parent'=> get_the_ID(),
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'orderby' => 'menu_order',
'posts_per_page' => -1,
'order' => 'DESC',
'exclude' => get_post_thumbnail_id()
)
);
var_export( $gallery_items );
if ( $gallery_items ) {
$existing_items = array(); // Je crée un tableau pour stocker les ID des attachments déjà récupérés
foreach ( $gallery_items as $attachment ) {
// Je n'affiche l'image que si elle n'existe pas dans le tableau des attachments déjà récupérés
if ( ! in_array( $attachment->ID, $existing_items ) ) {
echo '<a href="' . wp_get_attachment_url( $attachment->ID ) . '" rel="lightbox">';
echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="cls-banquet-item w-25"></a>';
array_push( $existing_items, $attachment->ID ); // Je stocke l'ID de l'attachment affiché dans le tableau des attachments déjà récupéré
}
}
}
}Function de récupération de toutes les images d'un custom postarray (
46 => WP_Post::__set_state(array(
'ID' => 46,
'post_author' => '1',
'post_date' => '2020-04-09 14:46:31',
'post_date_gmt' => '2020-04-09 12:46:31',
'post_content' => '',
'post_title' => 'traiteur-1',
'post_excerpt' => '',
'post_status' => 'inherit',
'comment_status' => 'open',
'ping_status' => 'closed',
'post_password' => '',
'post_name' => 'traiteur-1-3',
'to_ping' => '',
'pinged' => '',
'post_modified' => '2020-04-09 14:46:31',
'post_modified_gmt' => '2020-04-09 12:46:31',
'post_content_filtered' => '',
'post_parent' => 45,
'guid' => 'http://localhost/sylvietraiteur/wp-content/uploads/2020/04/traiteur-1-3.jpg',
'menu_order' => 0,
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg',
'comment_count' => '0',
'filter' => 'raw',
)),
47 => WP_Post::__set_state(array(
'ID' => 47,
'post_author' => '1',
'post_date' => '2020-04-09 14:46:32',
'post_date_gmt' => '2020-04-09 12:46:32',
'post_content' => '',
'post_title' => 'traiteur-2',
'post_excerpt' => '',
'post_status' => 'inherit',
'comment_status' => 'open',
'ping_status' => 'closed',
'post_password' => '',
'post_name' => 'traiteur-2-3',
'to_ping' => '',
'pinged' => '',
'post_modified' => '2020-04-09 14:46:32',
'post_modified_gmt' => '2020-04-09 12:46:32',
'post_content_filtered' => '',
'post_parent' => 45,
'guid' => 'http://localhost/sylvietraiteur/wp-content/uploads/2020/04/traiteur-2-3.jpg',
'menu_order' => 0,
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg',
'comment_count' => '0',
'filter' => 'raw',
)),
48 => WP_Post::__set_state(array(
'ID' => 48,
'post_author' => '1',
'post_date' => '2020-04-09 14:46:33',
'post_date_gmt' => '2020-04-09 12:46:33',
'post_content' => '',
'post_title' => 'traiteur-3',
'post_excerpt' => '',
'post_status' => 'inherit',
'comment_status' => 'open',
'ping_status' => 'closed',
'post_password' => '',
'post_name' => 'traiteur-3-3',
'to_ping' => '',
'pinged' => '',
'post_modified' => '2020-04-09 14:46:33',
'post_modified_gmt' => '2020-04-09 12:46:33',
'post_content_filtered' => '',
'post_parent' => 45,
'guid' => 'http://localhost/sylvietraiteur/wp-content/uploads/2020/04/traiteur-3-3.jpg',
'menu_order' => 0,
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg',
'comment_count' => '0',
'filter' => 'raw',
)),
49 => WP_Post::__set_state(array(
'ID' => 49,
'post_author' => '1',
'post_date' => '2020-04-09 14:46:56',
'post_date_gmt' => '2020-04-09 12:46:56',
'post_content' => '',
'post_title' => 'traiteur-2-4',
'post_excerpt' => '',
'post_status' => 'inherit',
'comment_status' => 'open',
'ping_status' => 'closed',
'post_password' => '',
'post_name' => 'traiteur-2-4',
'to_ping' => '',
'pinged' => '',
'post_modified' => '2020-04-09 14:46:56',
'post_modified_gmt' => '2020-04-09 12:46:56',
'post_content_filtered' => '',
'post_parent' => 45,
'guid' => 'http://localhost/sylvietraiteur/wp-content/uploads/2020/04/traiteur-2-4.jpg',
'menu_order' => 0,
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg',
'comment_count' => '0',
'filter' => 'raw',
)),
50 => WP_Post::__set_state(array(
'ID' => 50,
'post_author' => '1',
'post_date' => '2020-04-09 14:46:57',
'post_date_gmt' => '2020-04-09 12:46:57',
'post_content' => '',
'post_title' => 'traiteur-3-4',
'post_excerpt' => '',
'post_status' => 'inherit',
'comment_status' => 'open',
'ping_status' => 'closed',
'post_password' => '',
'post_name' => 'traiteur-3-4',
'to_ping' => '',
'pinged' => '',
'post_modified' => '2020-04-09 14:46:57',
'post_modified_gmt' => '2020-04-09 12:46:57',
'post_content_filtered' => '',
'post_parent' => 45,
'guid' => 'http://localhost/sylvietraiteur/wp-content/uploads/2020/04/traiteur-3-4.jpg',
'menu_order' => 0,
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg',
'comment_count' => '0',
'filter' => 'raw',
)),
)Résultat obtenu avec le var_dump()C’est dans du code, car j’ai souhaité rendre le résultat lisible. 🙂
Au cas-où, sinon ça donne ça :
array ( 46 => WP_Post::__set_state(array( ‘ID’ => 46, ‘post_author’ => ‘1’, ‘post_date’ => ‘2020-04-09 14:46:31’, ‘post_date_gmt’ => ‘2020-04-09 12:46:31’, ‘post_content’ => », ‘post_title’ => ‘traiteur-1’, ‘post_excerpt’ => », ‘post_status’ => ‘inherit’, ‘comment_status’ => ‘open’, ‘ping_status’ => ‘closed’, ‘post_password’ => », ‘post_name’ => ‘traiteur-1-3’, ‘to_ping’ => », ‘pinged’ => », ‘post_modified’ => ‘2020-04-09 14:46:31’, ‘post_modified_gmt’ => ‘2020-04-09 12:46:31’, ‘post_content_filtered’ => », ‘post_parent’ => 45, ‘guid’ => ‘http://localhost/sylvietraiteur/wp-content/uploads/2020/04/traiteur-1-3.jpg’, ‘menu_order’ => 0, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image/jpeg’, ‘comment_count’ => ‘0’, ‘filter’ => ‘raw’, )), 47 => WP_Post::__set_state(array( ‘ID’ => 47, ‘post_author’ => ‘1’, ‘post_date’ => ‘2020-04-09 14:46:32’, ‘post_date_gmt’ => ‘2020-04-09 12:46:32’, ‘post_content’ => », ‘post_title’ => ‘traiteur-2’, ‘post_excerpt’ => », ‘post_status’ => ‘inherit’, ‘comment_status’ => ‘open’, ‘ping_status’ => ‘closed’, ‘post_password’ => », ‘post_name’ => ‘traiteur-2-3’, ‘to_ping’ => », ‘pinged’ => », ‘post_modified’ => ‘2020-04-09 14:46:32’, ‘post_modified_gmt’ => ‘2020-04-09 12:46:32’, ‘post_content_filtered’ => », ‘post_parent’ => 45, ‘guid’ => ‘http://localhost/sylvietraiteur/wp-content/uploads/2020/04/traiteur-2-3.jpg’, ‘menu_order’ => 0, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image/jpeg’, ‘comment_count’ => ‘0’, ‘filter’ => ‘raw’, )), 48 => WP_Post::__set_state(array( ‘ID’ => 48, ‘post_author’ => ‘1’, ‘post_date’ => ‘2020-04-09 14:46:33’, ‘post_date_gmt’ => ‘2020-04-09 12:46:33’, ‘post_content’ => », ‘post_title’ => ‘traiteur-3’, ‘post_excerpt’ => », ‘post_status’ => ‘inherit’, ‘comment_status’ => ‘open’, ‘ping_status’ => ‘closed’, ‘post_password’ => », ‘post_name’ => ‘traiteur-3-3’, ‘to_ping’ => », ‘pinged’ => », ‘post_modified’ => ‘2020-04-09 14:46:33’, ‘post_modified_gmt’ => ‘2020-04-09 12:46:33’, ‘post_content_filtered’ => », ‘post_parent’ => 45, ‘guid’ => ‘http://localhost/sylvietraiteur/wp-content/uploads/2020/04/traiteur-3-3.jpg’, ‘menu_order’ => 0, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image/jpeg’, ‘comment_count’ => ‘0’, ‘filter’ => ‘raw’, )), 49 => WP_Post::__set_state(array( ‘ID’ => 49, ‘post_author’ => ‘1’, ‘post_date’ => ‘2020-04-09 14:46:56’, ‘post_date_gmt’ => ‘2020-04-09 12:46:56’, ‘post_content’ => », ‘post_title’ => ‘traiteur-2-4’, ‘post_excerpt’ => », ‘post_status’ => ‘inherit’, ‘comment_status’ => ‘open’, ‘ping_status’ => ‘closed’, ‘post_password’ => », ‘post_name’ => ‘traiteur-2-4’, ‘to_ping’ => », ‘pinged’ => », ‘post_modified’ => ‘2020-04-09 14:46:56’, ‘post_modified_gmt’ => ‘2020-04-09 12:46:56’, ‘post_content_filtered’ => », ‘post_parent’ => 45, ‘guid’ => ‘http://localhost/sylvietraiteur/wp-content/uploads/2020/04/traiteur-2-4.jpg’, ‘menu_order’ => 0, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image/jpeg’, ‘comment_count’ => ‘0’, ‘filter’ => ‘raw’, )), 50 => WP_Post::__set_state(array( ‘ID’ => 50, ‘post_author’ => ‘1’, ‘post_date’ => ‘2020-04-09 14:46:57’, ‘post_date_gmt’ => ‘2020-04-09 12:46:57’, ‘post_content’ => », ‘post_title’ => ‘traiteur-3-4’, ‘post_excerpt’ => », ‘post_status’ => ‘inherit’, ‘comment_status’ => ‘open’, ‘ping_status’ => ‘closed’, ‘post_password’ => », ‘post_name’ => ‘traiteur-3-4’, ‘to_ping’ => », ‘pinged’ => », ‘post_modified’ => ‘2020-04-09 14:46:57’, ‘post_modified_gmt’ => ‘2020-04-09 12:46:57’, ‘post_content_filtered’ => », ‘post_parent’ => 45, ‘guid’ => ‘http://localhost/sylvietraiteur/wp-content/uploads/2020/04/traiteur-3-4.jpg’, ‘menu_order’ => 0, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image/jpeg’, ‘comment_count’ => ‘0’, ‘filter’ => ‘raw’, )), )
11 avril 2020 à 18 h 20 min #2338144Super merci. Est-ce que tu pourrais tester pour chaque élément du tableau la valeur du champ
guid
(l’url de l’image) et dire si tu vois des doublons ?Si oui le problème vient d’image qui ont été ajoutés supprimées et qui sont toujours rattachées à l’article. SI c’est ça on verra quoi faire 🙂
11 avril 2020 à 18 h 36 min #2338146Alors, à en constater le résultat, l’ID 46, n’a pas de doublon.
Par contre, l’ID 47 fait doublon avec l’ID 48, et l’ID 49 fait doublon avec l’ID 50.
Je ne sais si c’est ce que tu voulais ?
Mais effectivement il y a bien des images qui se répètent.- Cette réponse a été modifiée le il y a 4 années et 9 mois par mumu51.
12 avril 2020 à 11 h 55 min #2338205?? 🙂
-
AuteurMessages
- Vous devez être connecté pour répondre à ce sujet.