从自定义查询中获取图像标题和Alt

时间:2017-12-20 作者:Wouter

我认为对于熟悉WordPress和PHP的人来说,这将是一个简单的问题。我不是PHP大师,所以我问你们!我们想要的很简单。我们的图片有标题和alt desc.但我们想在主题中呼应它们。

下面是应该在其中进行响应的代码。是的,我能够找到负责此操作的代码;-)。有什么建议吗?enter image description here

        while( have_posts() ) :

        the_post();

        $image = wp_get_attachment_image_src( (int)get_post_meta( get_the_ID() , "_thumbnail_id" , true ) , "collection" );
        $large_image = wp_get_attachment_image_src( (int)get_post_meta( get_the_ID() , "_thumbnail_id" , true ) , "original" );

        $terms_array = array();

        $terms = wp_get_post_terms( get_the_ID() , "brand" );

        $brand_image = "";

        $has_brand_names = array();

        foreach( $terms as $term ) :

            if( !$brand_image ) $brand_image = $brand_images["brand-".$term->term_id];

            $terms_array[] = "brand-" . $term->term_id;

            $has_brand_names[ $term->slug ] = true;

        endforeach;

        $terms = wp_get_post_terms( get_the_ID() , "collection_subcategory" );

        foreach( $terms as $term ) :

            $terms_array[] = "subcategory-" . $term->term_id;

        endforeach;

        printf( "<div class=\'shoe %s\' data-image=\'%s\' data-large-image=\'%s\' ><div class=\'shoe-outer\' ><div class=\'shoe-inner\' ><img src=\'%s\' ></div></div></div>", join(" ", $terms_array ), esc_attr( $image[0] ), esc_attr( $large_image[0] ), $brand_image );

    endwhile;

1 个回复
最合适的回答,由SO网友:Elex 整理而成

在代码的顶部,在$large_image, 您可以添加以下内容:

$image_alt = get_post_meta((int)get_post_meta( get_the_ID() , "_thumbnail_id" , true ), \'_wp_attachment_image_alt\', true);
$image_title = get_the_title((int)get_post_meta( get_the_ID() , "_thumbnail_id" , true ));
Thealt 附件已入库post_meta (_wp_attachment_image_alt), 标题是post_title.

然后替换printf().

EDIT:

printf( "<div class=\'shoe %s\' data-image=\'%s\' data-large-image=\'%s\' ><div class=\'shoe-outer\' ><div class=\'shoe-inner\' ><img src=\'%s\' alt=\'%s\' title=\'%s\' ></div></div></div>", join(" ", $terms_array ), esc_attr( $image[0] ), esc_attr( $large_image[0] ), $brand_image, $image_alt, $image_title );
告诉我这是不是工作:)

结束