[英]How to add a thumbnail in a post in wordpress?
我想在首頁的帖子中添加縮略圖。 但是不知道該怎么做。 我的主題不支持custom_field。 我在這里粘貼了類別代碼之一。
<div class="featured">
<h2>HEADLINES</h2>
<!--This is where the thumbnails are found for the homepage bottom section - note the custom field name for this image is "thumbnail". Recommended image size is 70x70, as the stylesheet is written for this size.-->
<?php $recent = new WP_Query("cat=3&showposts=3");
while($recent->have_posts()) : $recent->the_post();?>
<?php the_post_thumbnail(); ?>
<?php if( get_post_meta($post->ID, "thumbnail", true) ): ?>
<a href="<?php the_post_thumbnail() ?>" rel="bookmark"><imgstyle="float:left;margin:0px 10px 0px 0px;" src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" alt="alt text" /></a>
<?php else: ?>
<a href="<?php the_permalink() ?>" rel="bookmark"><imgstyle="float:left;margin:0px 10px 0px 0px;" src="<?php bloginfo('template_url'); ?>/images/thumbnail.jp" alt="Default thumbnail" /></a>
<?php endif; ?>
<b><a href="<?php the_permalink() ?>" ><?php the_title(); ?></a></b>
<?php the_content_limit(80, ""); ?>
<div style="border-bottom:1px dotted #AFAFAF; margin-bottom:10px; padding:0px 0px 10px 0px; clear:both;"></div>
<?php endwhile; ?>
我將擴展名jpg重命名為jp coz stackoverflow,不允許添加圖像。 我也變了
在WordPress 2.9之前,縮略圖必須是自定義字段。 從那時起,添加了本機支持。
在法典上閱讀相關內容: http : //codex.wordpress.org/Post_Thumbnails
簡而言之,將其添加到主題的functions.php文件中:
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
}
然后,將此添加到您希望縮略圖出現的主題:
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.