【发布时间】:2014-09-13 04:49:03
【问题描述】:
这是我的问题。我希望所有缩略图都链接到帖子
除了 single.php 中的主循环缩略图链接到外部网站
但也为 single.php 中的第二个循环“相关帖子缩略图”链接到帖子
下面的“代码 A”仅实现前 2 个目标。 如何在 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 中的缩略图链接到外部网站
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(); ?>
【问题讨论】:
标签: php thumbnails conditional-statements wordpress