0

基本上我想要完成的是如果有超过 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') {
4

3 回答 3

0

你为什么不做这样的事情来检查你帖子中附加图片的数量?不过,这假设您的图片已附加到您的帖子中。

于 2012-09-02T04:21:45.047 回答
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>' ;
 } 
于 2012-09-02T05:32:25.490 回答
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>';
 }
}

感谢大家的帮助!

于 2012-09-06T11:19:29.937 回答