[英]How to retrieve an image from a post and display it before excerpt of a post in WordPress?
[英]How to display the feature image of each post in WordPress?
我已使用以下代碼嘗試顯示每個帖子的特色圖片,但未顯示任何內容:
<div class="thumbnail-img">
<?php
$lastBlog = new WP_Query('type=post&posts_per_page=2&offset=1');
if ($lastBlog->has_post_thumbnail()) {
while($lastBlog->has_post_thumbnail()) {
$lastBlog->the_post_thumbnail();
} ?>
<?php get_template_part('content-image', get_the_post_thumbnail());
}
?>
</div>
<br>
<?php
if( $lastBlog->have_posts()):
while($lastBlog->have_posts()): $lastBlog->the_post(); ?>
<?php get_template_part('content-title', get_post_format()); ?>
<?php endwhile;
endif;
wp_reset_postdata();
?>
</div>
我想在每個帖子標題的上方添加特色圖片。 我該如何解決?
我真的不能具體說明您的模板結構(template-part content-title ??),但使用通用示例,以下將在可用的地方顯示特色圖片;
functions.php
if ( ! function_exists( 'mytheme_setup' ) ) :
function mytheme_setup() {
/*
* Enable support for Post Thumbnails on posts and pages.
*
* See: https://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
*/
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 825, 510, true );
}
endif;
add_action( 'after_setup_theme', 'mytheme_setup' );
您的內容模板頁面(content.php,模板頁面等。)
// WP_Query arguments
$args = array (
'nopaging' => false,
'posts_per_page' => '2',
'offset' => '1',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<article>
<?php if ( has_post_thumbnail() ) : ?>
<div class="post-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endif; ?>
<div class="post-title">
<?php echo '<h2>' . get_the_title() . '</h2>'; ?>
</div>
</article>
<?php
}
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
echo "NADA";
}
<?php
$lastBlog = new WP_Query('type=post&posts_per_page=2&offset=1');
if( $lastBlog->have_posts()):
while($lastBlog->have_posts()): $lastBlog->the_post(); ?>
<div class="title"><?php echo get_the_title(); ?></div>
<br />
<div class="thumbnail-img"><?php echo the_post_thumbnail();?></div>
<br />
<?php
endwhile;
endif;
wp_reset_postdata();
?>
試試這個應該很好
如果您想要圖片網址,請使用此
$thumb_image=wp_get_attachment_url( get_post_thumbnail_id() );
而您想獲得直接圖像,然后在這里獲得不同的圖像
the_post_thumbnail( 'thumbnail' ); // Thumbnail (150 x 150 hard cropped)
the_post_thumbnail( 'medium' ); // Medium resolution (300 x 300 max height 300px)
the_post_thumbnail( 'medium_large' ); // Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)
the_post_thumbnail( 'large' ); // Large resolution (1024 x 1024 max height 1024px)
the_post_thumbnail( 'full' ); // Full resolution (original size uploaded)
//With WooCommerce
the_post_thumbnail( 'shop_thumbnail' ); // Shop thumbnail (180 x 180 hard cropped)
the_post_thumbnail( 'shop_catalog' ); // Shop catalog (300 x 300 hard cropped)
the_post_thumbnail( 'shop_single' ); // Shop single (600 x 600 hard cropped)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.