[英]Wordpress get the attachment link for an image
The images that I'm querying from my wordpress media gallery are appearing correctly. 我从wordpress媒体库中查询的图像正确显示。 But, I also want them to link to my custom attachment page and I can't get it to work
但是,我也希望他们链接到我的自定义附件页面,我无法让它工作
<?php
$query_images_args = array(
'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,
);
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= wp_get_attachment_url( $image->ID );
}
foreach ($images as $img) {
$url = get_attachment_link($img->ID);
echo '<a href="' . $url . '">' . '<img src="' . $img . '" alt="" />' . '</a>';
}
?>
Skip the second foreach loop. 跳过第二个foreach循环。 You have everything you need in the first one:
在第一个中,您拥有所需的一切:
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= wp_get_attachment_url( $image->ID );
$url = get_attachment_link($image->ID);
echo '<a href="' . $url . '">' . '<img src="' . wp_get_attachment_url( $image->ID ) . '" alt="" />' . '</a>';
}
wp_get_attachment_url()
returns the URI of the image, see http://codex.wordpress.org/Function_Reference/wp_get_attachment_url ; wp_get_attachment_url()
返回图像的URI,参见http://codex.wordpress.org/Function_Reference/wp_get_attachment_url ; you were trying to get the ID from this non-object. 你试图从这个非对象中获取ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.