1. 云栖社区>
  2. PHP教程>
  3. 正文

Return the alt tags on images using Custom Field Suite

作者:用户 来源:互联网 时间:2017-12-01 18:15:45

Return the alt tags on images using Custom Field Suite - 摘要: 本文讲的是Return the alt tags on images using Custom Field Suite, If you’re using the WordPress Custom Field Suite plugin in your theme to upload and display images you should make sur

If you’re using the WordPress Custom Field Suite plugin in your theme to upload and display images you should make sure that you’re adding the alt and title (optionally) to the image being displayed on your site. Alt tags are important for SEO of course..

In this example we setup a “loop” field and within the loop we add another field for the image upload. Make sure to set the return value to “Attachment ID” on the upload field. This setup is great because now you can upload as many images as you want.

Return the alt tags on images using Custom Field Suite

Next add this PHP to your theme. This code will get the alt and title tags and display both with the image correctly.

<?php$upload_path = content_url() . '/uploads/'; $fields = CFS()->get('logos_row');foreach ($fields as $field) { $logo_image_ID = $field['logo'];$logo_image_ALT = get_post_meta($logo_image_ID, '_wp_attachment_image_alt', true);$logo_image_TITLE = get_the_title($logo_image_ID);$logo_image_URL_data = wp_get_attachment_metadata($logo_image_ID, true);$logo_image_URL = $logo_image_URL_data["file"]; echo '<img src="';echo $upload_path . $logo_image_URL;echo '" title="'.$logo_image_TITLE.'" alt="';echo $logo_image_ALT;echo '">';}?>

Make sure you edit the 2 items highlighted below with the name of your loop and image upload.

Return the alt tags on images using Custom Field Suite

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。