如何在wordpress中获取缩略图
How to get thumbnail image in wordpress
我有这样的代码,
$output .= '<div class="feature-course" '.$style.'>';
$output .= '<h3><a href="' . get_permalink(). '">' . get_the_title(). '</a></h3>';
$output .= '<p>' . the_excerpt_max_charlength(70). '</p>';
$output .= '<a class="btn-featue" href="' . get_permalink(). '">' . __('View Course', 'themeum-lms'). ' <i class="fa fa-long-arrow-right"></i></a>';
我需要添加图像功能,我该怎么做?你能给我推荐一下吗
get_the_post_thumbnail ( int $post_id = null, string|array $size = 'post-thumbnail', string|array $attr = '' )
更多信息在这里:https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/
<?php wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); ?>
<?php echo wp_get_attachment_image( 1 ); ?>
<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li>';
echo wp_get_attachment_image( $attachment->ID, 'full' );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
endwhile; endif; ?>
</ul>
https://codex.wordpress.org/Function_Reference/wp_get_attachment_image
您可以使用以下代码获取缩略图。
the_post_thumbnail(); // without parameter -> 'post-thumbnail'
the_post_thumbnail( 'thumbnail' ); // Thumbnail (default 150px x 150px max)
the_post_thumbnail( 'medium' ); // Medium resolution (default 300px x 300px max)
the_post_thumbnail( 'large' ); // Large resolution (default 640px x 640px max)
the_post_thumbnail( 'full' ); // Full resolution (original size uploaded)
the_post_thumbnail( array(100, 100) ); // Other resolutions
更多请参考https://codex.wordpress.org/Function_Reference/the_post_thumbnail
============================
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
echo get_the_post_thumbnail($post->ID, 'thumbnail');
echo '</a>';
}
试试这个
$output .= '<div class="feature-course" '.$style.'>';
$output .= '<h3><a href="' . get_permalink(). '">' . get_the_title(). '</a></h3>';
//Display Full image thubnail with link
$output .= '<a href="'.get_permalink().'">'.get_the_post_thumbnail(get_the_ID(),'full').'</a>';
// Retrieve Thumbnail URL and use it on src attribute
$output .= '<a href="'.get_permalink().'"><img src="'.wp_get_attachment_url( get_post_thumbnail_id(get_the_ID()) ).'" alt="Yow" title="Yow" /></a>';
$output .= '<p>' . the_excerpt_max_charlength(70). '</p>';
$output .= '<a class="btn-featue" href="' . get_permalink(). '">' . __('View Course', 'themeum-lms'). ' <i class="fa fa-long-arrow-right"></i></a>';
我有这样的代码,
$output .= '<div class="feature-course" '.$style.'>';
$output .= '<h3><a href="' . get_permalink(). '">' . get_the_title(). '</a></h3>';
$output .= '<p>' . the_excerpt_max_charlength(70). '</p>';
$output .= '<a class="btn-featue" href="' . get_permalink(). '">' . __('View Course', 'themeum-lms'). ' <i class="fa fa-long-arrow-right"></i></a>';
我需要添加图像功能,我该怎么做?你能给我推荐一下吗
get_the_post_thumbnail ( int $post_id = null, string|array $size = 'post-thumbnail', string|array $attr = '' )
更多信息在这里:https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/
<?php wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); ?>
<?php echo wp_get_attachment_image( 1 ); ?>
<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li>';
echo wp_get_attachment_image( $attachment->ID, 'full' );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
endwhile; endif; ?>
</ul>
https://codex.wordpress.org/Function_Reference/wp_get_attachment_image
您可以使用以下代码获取缩略图。
the_post_thumbnail(); // without parameter -> 'post-thumbnail'
the_post_thumbnail( 'thumbnail' ); // Thumbnail (default 150px x 150px max)
the_post_thumbnail( 'medium' ); // Medium resolution (default 300px x 300px max)
the_post_thumbnail( 'large' ); // Large resolution (default 640px x 640px max)
the_post_thumbnail( 'full' ); // Full resolution (original size uploaded)
the_post_thumbnail( array(100, 100) ); // Other resolutions
更多请参考https://codex.wordpress.org/Function_Reference/the_post_thumbnail
============================
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
echo get_the_post_thumbnail($post->ID, 'thumbnail');
echo '</a>';
}
试试这个
$output .= '<div class="feature-course" '.$style.'>';
$output .= '<h3><a href="' . get_permalink(). '">' . get_the_title(). '</a></h3>';
//Display Full image thubnail with link
$output .= '<a href="'.get_permalink().'">'.get_the_post_thumbnail(get_the_ID(),'full').'</a>';
// Retrieve Thumbnail URL and use it on src attribute
$output .= '<a href="'.get_permalink().'"><img src="'.wp_get_attachment_url( get_post_thumbnail_id(get_the_ID()) ).'" alt="Yow" title="Yow" /></a>';
$output .= '<p>' . the_excerpt_max_charlength(70). '</p>';
$output .= '<a class="btn-featue" href="' . get_permalink(). '">' . __('View Course', 'themeum-lms'). ' <i class="fa fa-long-arrow-right"></i></a>';