我有以下代码:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => 12,
'post_status' => null
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li><a href="'.get_permalink( $attachment->ID ).'">';
echo wp_get_attachment_image( $attachment->ID, array('100', '100') );
echo '</a></li>';
}
}
?>
此脚本的要点是显示最后添加的12张照片(大拇指)。这很完美。但我想添加第二个功能 - 链接到它来自的页面(通常是本地库嵌入帖子/页面)
问题是在这种情况下链接已损坏。它总是链接到第一篇文章。我做错了什么?
答案 0 :(得分:0)
以下是最终版本:)
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => 12,
'post_status' => null
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$url = get_permalink( $attachment->ID );
echo '<li><a href="'.strstr($url, '/attachment', true).'">';
echo wp_get_attachment_image( $attachment->ID, array('100', '100') );
echo '</a></li>';
}
}
?>
/ attachment是我们要从网址中删除所有内容的起点。