基本上我想要完成的是如果有超过1个img它将回显下面的语句,所以我基本上可以翻转说点击查看更多。另一个问题是,如果我要将其更改为“a href”并将回调链接到post_id,我将如何将其链接到帖子本身。
非常感谢任何帮助。
function catch_images() {
global $post, $posts;
$first_image = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/ii', $post->post_content, $matches);
$first_image = $matches [2] [0];
if ($output == '2') {
echo '<div class="seemore"><img src="images/magglass.png"></div><div class="seemoretext">See More</div>';
}
}
我感觉很愚蠢,我应该放下以下内容:
if ($output > '2') {
答案 0 :(得分:0)
为什么不执行this之类的操作来检查帖子中附加图片的数量?这假设您的图片已附加到您的帖子上。
答案 1 :(得分:0)
好吧,正如评论中所说,如果它有2个以上的图像,我真的不明白你要链接到图像本身?到附件页面?到另一个帖子? 无论如何,这样的事情应该有效 - 阅读并执行评论以满足您的需求..
(我不理解的第二个问题......)
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'published', // or NULL
//'post_mime_type' => 'image', // only if you want images alone
'post_parent' => $post->ID
);
$attachments = get_posts($args);
$counter = 0;
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
if (!$counter == 1) {
echo wp_get_attachment_image($attachment->ID);
}
else {
// uncomment the following line if you want a LIST of all following attachments and then delete the marked line
//echo '<a href = ' . wp_get_attachment_url($attachment->ID) . '> see more (image'. $counter .') </a>' ;
}
++$counter;
}
// Delete this line if you have more than 2 images, otherwise it will show the last one only
echo '<a href = ' . wp_get_attachment_url($attachment->ID) . '> see more (image'. $counter .') </a>' ;
}
答案 2 :(得分:0)
以下是我自己的问题的答案,如果给定帖子中有超过1个img src标记,它将回显。
function catch_images() {
global $post, $posts;
$first_image = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/ii', $post->post_content, $matches);
$first_image = $matches [0] [1];
if ($output > '2') {
echo '<div class="seemore"><div class="seemoreimg"><img src="images/magglass.png"></div><div class="seemoretext">See More</div></div>';
}
}
感谢大家的帮助!