single.php中2个循环的缩略图和条件链接

single.php中2个循环的缩略图和条件链接,php,thumbnails,conditional-statements,wordpress,Php,Thumbnails,Conditional Statements,Wordpress,这是我的问题。我希望所有的缩略图都能链接到这篇文章 除了single.php中链接到外部网站的主循环缩略图之外 但是对于single.php中的第二个循环相关帖子缩略图,也可以链接到帖子 下面的代码A只实现了前两个目标。 如何在single.php相关文章的缩略图中进行第二次循环,以链接到文章而不是外部网站 我正在使用: the_post_thumbnail('large') for main loop in single.php the_post_thumbnail('thumbnail')

这是我的问题。我希望所有的缩略图都能链接到这篇文章 除了single.php中链接到外部网站的主循环缩略图之外 但是对于single.php中的第二个循环相关帖子缩略图,也可以链接到帖子

下面的代码A只实现了前两个目标。 如何在single.php相关文章的缩略图中进行第二次循环,以链接到文章而不是外部网站

我正在使用:

the_post_thumbnail('large') for main loop in single.php
the_post_thumbnail('thumbnail') for second loop in single.php
the_post_thumbnail('medium') for everything else  
。 代码A:除了single.php中的缩略图链接到外部网站外,所有缩略图都链接到post

function my_post_image_html( $html, $post_id, $post_image_id ) { 
if ( is_single() ) { 
    $name = get_post_meta($post_id, 'externalurl', true);
    if( $name ) {           
        $html = '<a href="'.$name.'" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
    }
    return $html;
}
else 
{
    $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
    return $html;
}
}

add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );

代码B:single.php相关帖子缩略图中的第二个循环

<?php 
$args = array(
    'posts_per_page' => 6,
    'orderby' => rand
);
$the_query = new WP_Query( $args ); 
?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php endwhile;?>

<?php wp_reset_postdata(); ?> 

第二步,您的代码在我的系统上运行良好。permalinks是正确的。但是,在第二个循环中还有一个额外的循环:

<?php the_post_thumbnail('thumbnail') ); ?>
此外,在第二次lop中,将所需大小从缩略图更改为大:

 <?php the_post_thumbnail('large'); ?>

移除额外的时没有更改。目前,代码将single.php中主循环和第二循环中的缩略图链接到外部网站。有没有办法让第二个循环链接到帖子?或者,是否有一种方法可以在其他缩略图大小链接到帖子时,对帖子的缩略图“大”链接到外部网站执行条件语句?您可以通过在过滤器中添加第四个参数来检查请求的大小,即选中编辑。
 <?php the_post_thumbnail('large'); ?>