أنا أستخدم ملف attachment.php لعرض نسخ كبيرة من الصور التي تم النقر عليها في مكان آخر. أرغب في سحب نص بديل الصورة كتعليق أسفل الصورة باستخدام javascript ، ولكن لا يتم تضمين نص بديل عند استخدام wp_get_attachment_image_src (). لا أعتقد أن WP لها وظيفة لاستعادتها ، لذلك أحتاج إلى وظيفتي. لكتابة هذه الوظيفة أحتاج إلى معرفتها ... أين يتم تخزين نص بديل لصورة ما؟
تستخدم صفحة المرفقات الخاصة بي wp_get_attachment_image_src()
، والتي لا تتضمن النص البديل.
<div class = "entry">
<?php
if ( wp_attachment_is_image( $post->id ) ) :
$att_image = wp_get_attachment_image_src( $post->id, "large");?>
<a href="<?php echo wp_get_attachment_url($post->id); ?>"
title="<?php the_title(); ?>"
rel="attachment">
<img class="attached_img"
src="<?php echo $att_image[0];?>"
width="<?php echo $att_image[1];?>"
height="<?php echo $att_image[2];?>"
class="attachment-medium"
alt="<?php $post->post_excerpt; ?>" />
</a>
} <?php endif;?>
</div>
هذا يبين:
<div class = "entry">
<a href="http://www.example.com/wp-content/uploads/2010/07/photo_namejpg"
title="My_Photo_Title"
rel="attachment">
<img class="attached_img"
src="http://www.example.com/wp-content/uploads/2010/07/photo_name_and_size.jpg"
width="393"
height="500"
class="attachment-medium"
alt="" />
</a>
</div>
أدرك أنه يتم استدعاء $post->post_excerpt
في الشفرة أعلاه ، لكنني لست متأكدًا مما يجب استبداله بالحصول على سمة alt للصورة.
لقد أجريت مؤخرًا بعض الأبحاث لمشروع عميل مؤخراً حتى {lo-and-behold _ يمكنني استخدامه هنا!
بعد النص ، سترى قائمة مصنفة تضم معظم (كل شيء؟) من وظائف معالجة الصور من داخل WordPress 3.0.1 (قمت بتجميعها في بعض مظاهر الترتيب ولكن لا تضع الكثير من المصداقية في تصنيفي.)
على أي حال ،الإجابة على ما (أعتقد) تحتاج بدلاً من ما طلبت(حسنا ، سأجيب على ذلك أيضًا ، في النهاية) أعتقد ما تحتاجه هو وظيفة wp_get_attachment_image()
والتي ستُرجع سلسلة HTML تحتوي على هذه السمات:
'src'
،'class'
،'alt'
و'title'
.لذا ، إليك وظائف معالجة الصور في WordPress لمرجعك ولغيرك ({القفز أدناه للإجابة على سؤالك الدقيق):
set_post_thumbnail_size( $width = 0, $height = 0, $crop = FALSE )
add_image_size( $name, $width = 0, $height = 0, $crop = FALSE )
get_intermediate_image_sizes()
wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 )
get_attached_file( $attachment_id, $unfiltered = false )
is_local_attachment($url)
update_attached_file( $attachment_id, $file )
wp_attachment_is_image( $post_id = 0 )
wp_count_attachments( $mime_type = '' )
wp_delete_attachment( $post_id, $force_delete = false )
wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '')
wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false)
wp_get_attachment_metadata( $post_id = 0, $unfiltered = false )
wp_get_attachment_thumb_file( $post_id = 0 )
wp_get_attachment_thumb_url( $post_id = 0 )
wp_get_attachment_url( $post_id = 0 )
wp_insert_attachment($object, $file = false, $parent = 0)
wp_update_attachment_metadata( $post_id, $data )
wp_match_mime_types($wildcard_mime_types, $real_mime_types)
wp_mime_type_icon( $mime = 0 )
wp_post_mime_type_where($post_mime_types, $table_alias = '')
wp_load_image( $file )
image_constrain_size_for_editor($width, $height, $size = 'medium')
image_downsize($id, $size = 'medium')
image_get_intermediate_size($post_id, $size='thumbnail')
image_hwstring($width, $height)
image_make_intermediate_size($file, $width, $height, $crop=false)
image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 )
image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false)
كما وعدت يتم تخزين النص 'alt'
النص كسلسلة في wp_postmeta
مع meta_key الخاص بـ '_wp_attachment_image_alt'
.
كما تعلمون بالفعل ، يمكنك تحميله مع get_post_meta()
بسيط مثل:
$alt_text = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
ضع في اعتبارك النظر إلى wp_prepare_attachment_for_js( $attachment )
، حيث $attachment
هو كائن WP_Post الخاص بالمرفق نفسه.
هذه وظيفة من نوع "بالوعة المطبخ" ، ولكنها توفر تجزئة رائعة جدًا مع الكثير من البيانات الوصفية ، بما في ذلك "البديل":
$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,
);
هذا مفيد بشكل خاص (كما يوحي الاسم) ، لإرسال ملف تعريف صورة المرفق إلى عرض wp.media عبر wp_send_ajax()
، لكن هذا لا يعني أنه لا يمكنك استخدامه لأغراض أخرى.
أحب الاستبعاد بعيدًا عن حقل التعريف المنشور _wp_attachment_image_alt
، في حالة تغير طريقة استرداد النص البديل (غير مرجح ، ولكن يمكن تصوره).
أشعر أن هناك حالة لطريقة wp_get_attachment_image_alt()
.
Mike's answer هو الصحيح ، بالطبع ، ولكن $alt_text = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
قد ترجع سلسلة فارغة.
wp_get_attachment_image ، ومع ذلك ، يحصل دائمًا على نص بديل.
يطبق فريق Wordpress الخدعة التالية ، أولاً ، التحقق من post_except ، ثم الحصول على اللقب.
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 ));
}
لقد اكتشفت أن النص البديل للمرفقات تم تخزينه على ملف تعريف مخصص يسمى "_wp_attachment_image_alt"
وبالتالي ، بعد أن حصلت على معرف المرفق ، تمكنت من الحصول على نص بديل باستخدام هذا الرمز:
<?php echo get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ?>
إذا كنت تستخدم WP_Customize_Media_Control () سيعيد get_theme_mod () معرف المنشور ، ولكن إذا كنت تستخدم WP_Customize_Image_Control () الجديد ، فسوف يعرض get_theme_mod () عنوان url للصورة وهذا هو كيف تمكنت من الحصول على نص بديل من استخدام WP_Customize_Image_Control ()
إليكم كيف تمكنت من القيام بذلك. آمل أن يساعد هذا شخص هناك
// 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>
لإضافة إلى إجابة مايك ، قد يجد شخص ما هذا مفيدًا. قد تحتاج إلى الحصول على المعرف المحدد للمرفق ، بحيث يمكنك القيام بذلك عن طريق تمرير معرف المشاركة إلى get_post_thumbnail_id
مثال:
$the_img = wp_get_attachment_image( get_post_thumbnail_id( get_the_ID() ) );