Comment récupérer le texte alternatif d'une pièce jointe d'image?
5 réponses
- votes
-
- 2015-04-23
Pensez à regarder
wp_prepare_attachment_for_js( $attachment )
,où$attachment
est l'objet WP_Post de lapiècejointeelle-même.C'est unpeu unefonction "évier de cuisine",mais celafournit untrèsbon hachage avec unetonne demétadonnées,y compris 'alt':
$response = array( 'id' => $attachment->ID, 'title' => $attachment->post_title, 'filename' => wp_basename( $attachment->guid ), 'url' => $attachment_url, 'link' => get_attachment_link( $attachment->ID ), 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), 'author' => $attachment->post_author, 'description' => $attachment->post_content, 'caption' => $attachment->post_excerpt, 'name' => $attachment->post_name, 'status' => $attachment->post_status, 'uploadedTo' => $attachment->post_parent, 'date' => strtotime( $attachment->post_date_gmt ) * 1000, 'modified' => strtotime( $attachment->post_modified_gmt ) * 1000, 'menuOrder' => $attachment->menu_order, 'mime' => $attachment->post_mime_type, 'type' => $type, 'subtype' => $subtype, 'icon' => wp_mime_type_icon( $attachment->ID ), 'dateFormatted' => mysql2date( get_option('date_format'), $attachment->post_date ), 'nonces' => array( 'update' => false, 'delete' => false, 'edit' => false ), 'editLink' => false, 'meta' => false, );
Ceciestparticulièrement utile (comme sonnom l'indique),pourenvoyer laméta de l'image de lapiècejointe à une vue wp.media via
wp_send_ajax()
,mais celane signifiepas que vousne pouvezpas l'utiliser à d'autresfins.J'aime l'abstraction du champméta depublication
_wp_attachment_image_alt
,au cas où laméthodepour récupérer letexte alternatif changerait (peuprobable,mais concevable).Jepense cependant qu'ilexiste un caspour uneméthode
wp_get_attachment_image_alt()
.Consider looking at
wp_prepare_attachment_for_js( $attachment )
, where$attachment
is the WP_Post object of the attachment itself.This is a bit of a "kitchen sink" function, but it does provide a very nice hash with a ton of metadata, including 'alt':
$response = array( 'id' => $attachment->ID, 'title' => $attachment->post_title, 'filename' => wp_basename( $attachment->guid ), 'url' => $attachment_url, 'link' => get_attachment_link( $attachment->ID ), 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), 'author' => $attachment->post_author, 'description' => $attachment->post_content, 'caption' => $attachment->post_excerpt, 'name' => $attachment->post_name, 'status' => $attachment->post_status, 'uploadedTo' => $attachment->post_parent, 'date' => strtotime( $attachment->post_date_gmt ) * 1000, 'modified' => strtotime( $attachment->post_modified_gmt ) * 1000, 'menuOrder' => $attachment->menu_order, 'mime' => $attachment->post_mime_type, 'type' => $type, 'subtype' => $subtype, 'icon' => wp_mime_type_icon( $attachment->ID ), 'dateFormatted' => mysql2date( get_option('date_format'), $attachment->post_date ), 'nonces' => array( 'update' => false, 'delete' => false, 'edit' => false ), 'editLink' => false, 'meta' => false, );
This is particularly useful (as the name implies), for sending the attachment image meta to a wp.media View via
wp_send_ajax()
, but that doesn't mean you couldn't use it for other purposes.I like abstracting away from the
_wp_attachment_image_alt
post meta field, in case the method to retrieve the alt text ever changes (unlikely, but conceivable).I do feel that there's a case for a
wp_get_attachment_image_alt()
method however.-
Exactement ce queje cherchais.Quelqu'un a-t-il uneidée de saperformance?Avec autant de valeurs différentes qu'il récupère ...je me demande ...Exactly what i was looking for. Does anyone have an idea of its performance? With so many different values it retrieves... I wonder...
- 0
- 2015-07-16
- Larzan
-
@Larzan Jene m'inquiéteraispas desperformances - àmoins que vousn'obteniez des centaines de données d'imagesen mêmetemps ...@Larzan I wouldn't worry about performance - unless you're getting hundreds of images data at the same time...
- 0
- 2015-07-17
- Tom Auger
-
- 2015-02-11
La réponse de Mike est correcte,bien sûr,mais
$alt_text = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
peut renvoyer une chaîne vide.wp_get_attachment_image ,cependant,obtienttoujours un alt_text.
L'équipe Wordpress applique l'astuce suivante,d'aborden vérifiant lepost_except,puisen obtenant letitre.
if(empty($alt_text)) // If not, Use the Caption { $attachment = get_post($post->ID); $alt_text = trim(strip_tags( $attachment->post_excerpt )); } if(empty($alt_text)) // Finally, use the title { $attachment = get_post($post->ID); $alt_text = trim(strip_tags( $attachment->post_title )); }
Mike's answer is correct, of course, but
$alt_text = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
may return an empty string.wp_get_attachment_image, however, does always get an alt_text.
The Wordpress team applies the following trick, first, checking for the post_except, then obtaining the title.
if(empty($alt_text)) // If not, Use the Caption { $attachment = get_post($post->ID); $alt_text = trim(strip_tags( $attachment->post_excerpt )); } if(empty($alt_text)) // Finally, use the title { $attachment = get_post($post->ID); $alt_text = trim(strip_tags( $attachment->post_title )); }
-
- 2011-12-26
J'ai découvert que letexte alternatif despiècesjointes était stocké sur unemétapersonnalisée appelée "_wp_attachment_image_alt"
Donc,ayant l'ID de lapiècejointe,j'aipu obtenir letexte alternatif avec ce code:
<?php echo get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ?>
I found out that the Alt text for attachments was stored on a custom meta called "_wp_attachment_image_alt"
So having the attachment's Id, I was able to get the alt text with this code:
<?php echo get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ?>
-
- 2017-04-28
Si vous utilisez WP_Customize_Media_Control (),votreget_theme_mod () renverra l'identifiant de l'article,mais si vous utilisez lenouveau WP_Customize_Image_Control (),get_theme_mod () renverra l'url de l'image,c'est ainsi quej'aipu obtenir letexte alternatif deen utilisant WP_Customize_Image_Control ()
Voici commentj'aipu lefaire.J'espère que cela aide quelqu'un là-bas
// This is getting the image / url $feature1 = get_theme_mod('feature_image_1'); // This is getting the post id $feature1_id = attachment_url_to_postid($feature1); // This is getting the alt text from the image that is set in the media area $image1_alt = get_post_meta( $feature1_id, '_wp_attachment_image_alt', true );
Balisage
<a href="<?php echo $feature1_url; ?>"><img class="img-responsive center-block" src="<?php echo $feature1; ?>" alt="<?php echo $image1_alt; ?>"></a>
If you are using WP_Customize_Media_Control() your get_theme_mod() will return the post id but if you are using the new WP_Customize_Image_Control() the get_theme_mod() will return the image url this was how I was able to get the alt text from using the WP_Customize_Image_Control()
Here is how I was able to do it. Hope this helps someone out there
// This is getting the image / url $feature1 = get_theme_mod('feature_image_1'); // This is getting the post id $feature1_id = attachment_url_to_postid($feature1); // This is getting the alt text from the image that is set in the media area $image1_alt = get_post_meta( $feature1_id, '_wp_attachment_image_alt', true );
Markup
<a href="<?php echo $feature1_url; ?>"><img class="img-responsive center-block" src="<?php echo $feature1; ?>" alt="<?php echo $image1_alt; ?>"></a>
-
- 2017-08-10
Pour ajouter à la réponse de Mike,quelqu'unpourraittrouver cela utile.Vous devrezpeut-être obtenir l'ID spécifique de lapiècejointe,vouspouvez donc lefaireen transmettant l'ID depublication à
get_post_thumbnail_id
exemple:$the_img = wp_get_attachment_image( get_post_thumbnail_id( get_the_ID() ) );
To add to Mike's answer someone might find this useful. You might need to get the specific ID of the attachment, so you can do so by passing the Post ID to
get_post_thumbnail_id
example:$the_img = wp_get_attachment_image( get_post_thumbnail_id( get_the_ID() ) );
J'utilise unfichier attachment.phppour afficher degrandes versions d'images sur lesquelles on a cliqué ailleurs. Je voudraistirer letexte alternatif de l'imageen tant que légende sous l'image avecjavascript,mais letexte alternatifn'estpasinclus lorsque wp_get_attachment_image_src ()est utilisé. Jene pensepas que WP ait unefonctionpour le récupérer,doncj'aibesoin de lamienne. Pour écrire cettefonction,j'aibesoin de savoir ... Oùest stocké letexte alternatif d'uneimage?
Mapage depiècesjointes utilise
wp_get_attachment_image_src()
,quin'inclutpas letexte alternatif.Celamontre:
Je sais que le
$post->post_excerpt
est appelé dans le code ci-dessus,maisje ne saispaspar quoi le remplacerpour obtenir l'attribut alt de l'image.