Wie bekomme ich Bildtitel / Alt-Attribut?
-
-
Sie versuchen,das Post-Meta von "$ Anhang-> ID" abzurufen,aberich kann keine Informationen zum Objekt "$ Anhang"in Ihrem Code sehen.You are trying to get the post meta of `$attachment->ID` but I can not see any info about `$attachment` object in your code.
- 1
- 2015-07-01
- cybmeta
-
@cybmeta Ich habe diesen Codeausschnitt von hier http://wordpress.stackexchange.com/questions/185396/getting-the-image-title-alt-attribute-from-the-gallery-shortcode@cybmeta i have got this code snippet from here http://wordpress.stackexchange.com/questions/185396/getting-the-image-title-alt-attribute-from-the-gallery-shortcode
- 0
- 2015-07-01
- Nisha_at_Behance
-
Sie können auch Plugins wie 1) https://wordpress.org/plugins/imageseo/verwenden. 2) https://wordpress.org/plugins/auto-image-attributes-from-filename-with-bulk-updater/ 3) https://wordpress.org/plugins/auto-image-alt/Ich hoffees hilft!You can also use plugins such as 1) https://wordpress.org/plugins/imageseo/ 2) https://wordpress.org/plugins/auto-image-attributes-from-filename-with-bulk-updater/ 3) https://wordpress.org/plugins/auto-image-alt/ I hope it helps!
- 0
- 2020-02-24
- James
-
5 Antworten
- Stimmen
-
- 2019-03-04
Kam hierher,da dieser Beitrag zu den Top-Hitsin der Suchmaschinegehört,wennnach WordPress-Bild-Alt und -Titelgesucht wird.Daich ziemlich überraschtbin,dass keine der Antworteneine einfache Lösung zubieten scheint,die zum Titel der Fragepasst,werdeich das,wasichmir ausgedacht habe,fallen lassen und hoffen,dasses zukünftigen Lesern hilft
// An attachment/image ID is all that's needed to retrieve its alt and title attributes. $image_id = get_post_thumbnail_id(); $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE); $image_title = get_the_title($image_id);
Als Bonuserfahren Sie hier,wie Sieein Bild src abrufen.Mit den obengenannten Attributenist das alles,was wirbrauchen,um das Markupeines statischen Bildes zuerstellen.
$size = 'my-size' // Defaults to 'thumbnail' if omitted. $image_src = wp_get_attachment_image_src($image_id, $size)[0];
Came here as this post is among the top hits on the search engine when looking for WordPress image alt and title. Being rather surprised that none of the answers seem to provide a simple solution matching the question's title I'll drop what I came up with in the end hoping it helps future readers.
// An attachment/image ID is all that's needed to retrieve its alt and title attributes. $image_id = get_post_thumbnail_id(); $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE); $image_title = get_the_title($image_id);
As a bonus here's how to retrieve an image src. With the above attributes that's all we need to build a static image's markup.
$size = 'my-size' // Defaults to 'thumbnail' if omitted. $image_src = wp_get_attachment_image_src($image_id, $size)[0];
-
Ich verstehe den Unterschied zwischen Ihrer Antwort und den anderennicht.Das Problembei der Frage war,dass die Anhangs-IDnicht korrekt war,und dasist die Antwort.Außerdemerhalten Siein Ihrer Antwort die IDfür das Miniaturbild des aktuellen Beitrags,jedochnichtfür dengewünschten Anhang.esbeantwortet/löst alsonicht die Frage des OP.I don't get the difference in your answer and the others. The problem in the question was that the attachment ID was not correct, and that is the answer. Additionally, in your answer, you get the ID for the thumbnail of the current post, but not for the desired attachment. so it doesn't answer/solve the question of the OP.
- 0
- 2019-03-20
- cybmeta
-
Woher Sie Ihre Bild-IDbeziehen,liegtbei Ihnen.Abertrotzdem dankefür die Ablehnung.Besondersnett von dir,meine Antwortin deineeinzufügen.Where you get your image ID from is up to you. But thanks for the downvote anyways. Especially nice of you to paste my answer into yours.
- 0
- 2019-03-26
- leymannx
-
Sie habenmeine Gegenstimme dafür. Esistgenau so,wie Sieesbeschrieben haben.Top-Hitbei Google,gute Antwort.You have my upvote for this, It's exactly like you've described. Top hit on google, good answer.
- 1
- 2020-05-08
- user3135691
-
- 2015-07-01
Ihr Problemist,dass Sienicht die richtige Anhangs-IDfür die Funktionen
get_post_meta()
undget_the_title()
angeben.Diesist Ihr Code,um den
alt
des Bildes abzurufen:$image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true);
Undesist richtig,aber
$attachment->ID
istin Ihrem Codenicht definiert,sodass die Funktionnichts zurückgibt.Wenn Sie Ihren Code lesen,scheinen Sie die ID des Bildes als Metafeld zu speichern underhalten sie dannmit folgendem Code:
$image = get_post_meta(get_the_ID(), WPGRADE_PREFIX.'homepage_slide_image', true);
Angenommen,
$image->id
istin Ihrem Code korrekt,sollten Sie Folgendesersetzen:$image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true);
Mit:
$image_alt = get_post_meta( $image->id, '_wp_attachment_image_alt', true);
Damiterhalten Sie den
alt
,um den Titel zuerhalten:$image_title = get_the_title( $image->id );
Your problem is that you are not providing the correct attachment's ID to
get_post_meta()
andget_the_title()
functions.This is your code to get the
alt
of the image:$image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true);
And it is correct, but
$attachment->ID
is not defined in your code, so, the function does not return anything.Reading your code, it seems that you store the ID of the image as a meta field and then you get it with this code:
$image = get_post_meta(get_the_ID(), WPGRADE_PREFIX.'homepage_slide_image', true);
So, assuming that
$image->id
is correct in your code, you should replace this:$image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true);
With:
$image_alt = get_post_meta( $image->id, '_wp_attachment_image_alt', true);
That is for getting the
alt
, to get the title:$image_title = get_the_title( $image->id );
-
- 2019-01-29
Ich verwendein allenmeinen Themeneine Schnellfunktion,um Bildanhangsdaten abzurufen:
//get attachment meta if ( !function_exists('wp_get_attachment') ) { function wp_get_attachment( $attachment_id ) { $attachment = get_post( $attachment_id ); return array( 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content, 'href' => get_permalink( $attachment->ID ), 'src' => $attachment->guid, 'title' => $attachment->post_title ); } }
Hoffe das hilft!
I use a quick function in all my themes to get image attachment data:
//get attachment meta if ( !function_exists('wp_get_attachment') ) { function wp_get_attachment( $attachment_id ) { $attachment = get_post( $attachment_id ); return array( 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content, 'href' => get_permalink( $attachment->ID ), 'src' => $attachment->guid, 'title' => $attachment->post_title ); } }
Hope this helps!
-
- 2017-01-02
$image = get_post_meta(get_the_ID(), WPGRADE_PREFIX . 'homepage_slide_image', true); if (!empty($image)) { $image = json_decode($image); $image_id = $image->id; $img_meta = wp_prepare_attachment_for_js($image_id); $image_title = $img_meta['title'] == '' ? esc_html_e('Missing title','{domain}') : $img_meta['title']; $image_alt = $img_meta['alt'] == '' ? $image_title : $img_meta['alt']; $image_src = wp_get_attachment_image_src($image_id, 'blog-huge', false); echo '<img class="homepage-slider_image" src="' . $image_src[0] . '" alt="' . $image_alt . '" />'; }
Bittebeachten Sie,dassich Ihre
$image->id
nichtgetestet habe,sondernnur davon ausgegangenbin,dass Sie die richtige Anhangs-ID haben.Der Rest kommt von$img_meta
.Wenn altfehlt,verwenden wir den Bildtitel. Wenn der Titelfehlt,wird der Text "Fehlender Titel" angezeigt,um Sie zum Ausfüllen zubewegen.$image = get_post_meta(get_the_ID(), WPGRADE_PREFIX . 'homepage_slide_image', true); if (!empty($image)) { $image = json_decode($image); $image_id = $image->id; $img_meta = wp_prepare_attachment_for_js($image_id); $image_title = $img_meta['title'] == '' ? esc_html_e('Missing title','{domain}') : $img_meta['title']; $image_alt = $img_meta['alt'] == '' ? $image_title : $img_meta['alt']; $image_src = wp_get_attachment_image_src($image_id, 'blog-huge', false); echo '<img class="homepage-slider_image" src="' . $image_src[0] . '" alt="' . $image_alt . '" />'; }
please note that I did not test your
$image->id
, just assumed that you have the right attachment ID. The rest comes from$img_meta
. If alt is missing we are using image title, if title is missing you will see "Missing title" text to nudge you to fill it in. -
- 2017-04-29
Ok,ich habe die Antwortgefunden,dieniemandim Netz hat,nach derich seit Tagengesucht habe. Inmeinem Fallfunktioniert diesnur,wenn Ihr Design oder Plugin WP_Customize_Image_Control () verwendet. Wenn Sie WP_Customize_Media_Control () verwenden,gibt get_theme_mod () die ID undnicht die URL zurück.
Fürmeine Lösung habeich dieneuere Version WP_Customize_Image_Control ()
verwendetViele Beiträgein den Foren haben dieget_attachment_id (),dienichtmehrfunktioniert. Ich habe attachment_url_to_postid ()
verwendetSo konnteichesmachen. Hoffe das hilftjemandem da draußen
// 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>
Ok I found the answer that no one has on the net I been looking for days now. Keep in mine this only works if your theme or plugin is using the WP_Customize_Image_Control() if you are using WP_Customize_Media_Control() the get_theme_mod() will return the ID and not the url.
For my solution I was using the newer version WP_Customize_Image_Control()
A lot of posts on the forums have the get_attachment_id() which does not work anymore. I used attachment_url_to_postid()
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>
Inmeinem weißen Themaist kein alt-Attributfür den Home-Slider-Beitrag konfiguriert. Ich habe den Alternativtextfür das Bild über die Medienbibliotheksoberfläche hinzugefügt. Ich habe denfolgenden Code hinzugefügt,um den Alternativtext/das Alternativattribut anzuzeigen. Es wirdjedochnicht angezeigt:
Hierist der Code: