我有一个带有标题的图片的Wordpress网站(放在管理面板上)
我想将这些标题显示为图片下方的文字,但无法找到任何可以自动执行此操作的资源(通过标题标记,javascript等)
答案 0 :(得分:1)
JavaScript DOM API调用的组合可以轻松解决此问题:
segue
segue
window.addEventListener("DOMContentLoaded", function(){
// Get references to the needed DOM elements:
var images = document.querySelectorAll("img");
var parent = images[0].parentNode;
// Loop through all the images...
images.forEach(function(image){
// Create a <span> element
var span = document.createElement("span");
// Insert the <span> just prior to the current image
parent.insertBefore(span, image);
// Populate the <span> with the title of the image
image.previousElementSibling.textContent = image.title;
});
});
答案 1 :(得分:0)
要获取图像的元数据属性,您可以使用Wordpress API中的wp_get_attachment_metadata($attachment_id);
函数。
请查看此处的文档:https://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata
您将在返回值中看到有一个元数据数组,包括图像标题:
[image_meta] => Array
(
[aperture] => 5
[credit] =>
[camera] => Canon EOS-1Ds Mark III
[caption] =>
[created_timestamp] => 1323190643
[copyright] =>
[focal_length] => 35
[iso] => 800
[shutter_speed] => 0.016666666666667
[title] =>
)