WordPress получить заголовок изображения с помощью wp_get_attachment_image () - PullRequest
1 голос
/ 20 ноября 2011

Я пытаюсь добавить заголовок изображения к атрибуту alt изображений в галерее, но мой код не работает.Ниже приведен фрагмент шорткода галереи, модифицированный для реализации слайд-шоу.Внизу я использую wp_get_attachment_image() с $default_attr в качестве массива атрибутов, содержащих заголовок.Заголовок не отображается в HTML.

$id = intval($id);
    if ( 'RAND' == $order )
        $orderby = 'none';

    if ( !empty($include) ) {
        $include = preg_replace( '/[^0-9,]+/', '', $include );
        $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );

        $attachments = array();
        foreach ( $_attachments as $key => $val ) {
            $attachments[$val->ID] = $_attachments[$key];
        }
    } elseif ( !empty($exclude) ) {
        $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
        $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
    } else {
        $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
    }

    if ( empty($attachments) )
        return '';

    if ( is_feed() ) {
        $output = "\n";
        foreach ( $attachments as $att_id => $attachment )
            $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
        return $output;
    }
   $i = 0;


   $default_attr = array(
    'src'   => $src,
    'class' => "attachment-$size",
    'alt' => trim(strip_tags( $attachment->post_excerpt ))
    );

foreach ( $attachments as $attachment ) {
    <a href='".wp_get_attachment_url($attachment->ID)."'>".wp_get_attachment_image($attachment->ID, $size, false, $default_attr)."</a>      
}     

return $output;

Ответы [ 2 ]

0 голосов
/ 19 марта 2015

Пройдя через это во время поиска того же ответа, нашел его и хотел поделиться им:

$attachment=get_post($attachment_id);
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $image->post_content;

для вывода заголовка в формате WordPress (с добавлением разрывов и абзацев), вы можете просто использовать:

$caption = apply_filters('the_content', $caption);
0 голосов
/ 04 октября 2012

Вы можете использовать:

wp_get_attachment_url($attachment->post_title);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...