[英]How to make all images clickable? (In posts WordPress)
When I first created my site, I automatically made sure that all images had no links ( image_default_link_type = none
). 首次创建网站时,我会自动确保所有图像都没有链接( image_default_link_type = none
)。 Now I've done Lightbox, so I need to make all the images in all the posts clickable. 现在,我已经完成了Lightbox,所以我需要使所有帖子中的所有图像都可点击。
I need to translate all the images in all posts from the view: 我需要翻译视图中所有帖子中的所有图像:
<img src="https://example.com/wp-content/uploads/2018/04/Apple-iPhone-6-0.jpg" alt="" width="1500" height="1000" />
at 在
<a href="https://example.com/wp-content/uploads/2018/04/Apple-iPhone-6-0.jpg"> <img src="https://example.com/wp-content/uploads/2018/04/Apple-iPhone-6-0.jpg" alt="" width="1500" height="1000" /> </a>
How can I do that? 我怎样才能做到这一点?
There is a function: 有一个功能:
add_filter ('the_content', 'del_image_link');
function del_image_link ($ content) {
$ content =
preg_replace (array ('{a [^>] *> <img}', '{/> </a>}'), array ('<img', '/>'), $ content);
return $ content;
}
It works in reverse order, that is, it makes absolutely all images with links non-sticky. 它以相反的顺序工作,也就是说,它使所有带有链接的图像绝对不发粘。 I need to achieve the opposite effect. 我需要达到相反的效果。
$(document).ready(function(){
$("img").each(function() {
$(this).wrap("<a href="+$(this).attr('src')+"></a>");
});
});
You can add this script in footer and on document ready you can loop through all img
elements. 您可以将此脚本添加到页脚中,并准备好在文档中循环浏览所有img
元素。 In that loop using jQuery WRAP you can wrap the img
tag with a
tag. 在使用jQuery该循环WRAP你可以包装img
与标签a
标签。
#Using the code in the functions.php with the following: #
<?php
if ( has_post_thumbnail() ) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '">';
the_post_thumbnail( 'thumbnail' );
echo '</a>';
}
?>
# OR #
# Link all post thumbnails to the post permalink #
<?php
function wpdocs_post_image_html( $html, $post_id, $post_image_id ) {
$html = '<a href="' . get_permalink( $post_id ) . '" alt="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
return $html;
}
add_filter( 'post_thumbnail_html', 'wpdocs_post_image_html', 10, 3 );
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.