2012-10-18 23 views
0

所以我想添加一个缩略图到我的帖子,但我不能得到它的工作。WordPress的 - 在循环中发布缩略图

<?php get_header(); ?> 

<div id="main-content"> 
    <?php get_sidebar(); ?> 
    <?php 
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
     query_posts('posts_per_page=3&paged=' . $paged); 
    ?> 
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
     <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> 
      <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> 
      <?php the_post_thumbnail();?> 

      <div class="entry"> 
       <?php the_excerpt(); ?> 
       <a class="read-more" href="<?php the_permalink() ?>">Read More ...</a> 
      </div> 

      <?php include (TEMPLATEPATH . '/inc/meta.php'); ?> 

      <div class="postmetadata"> 
       <?php the_tags('Tags: ', ', ', '<br />'); ?> 
       Posted in <?php the_category(', ') ?> | 
       <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> 
      </div> 
     </div> 
    <?php endwhile; endif; ?> 

    <div class="navigation"> 
     <div class="next-posts"><?php next_posts_link('&laquo; Older Posts') ?></div> 
     <div class="prev-posts"><?php previous_posts_link('Newer Posts &raquo;') ?></div> 
    </div> 
</div> 
<!-- end div main-content --> 

<?php get_footer(); ?> 

在我的functions.php我添加 - add_theme_support('post-thumbnails');

它让我张贴的缩略图,当我发表的帖子的选项,但它并没有显示出来。

回答

1

您使用的是什么主题或父母主题?我通常不喜欢这样的循环中:

<?php 

if (function_exists('add_image_size')) { 
    add_image_size('custom-thumb', 180, 115, true); //add a custom image size 
} 

echo get_the_post_thumbnail(get_the_ID(), 'custom-thumb', $attr); //echo the thumbnail with the new custom image size 

?> 
1
<?php 
if (has_post_thumbnail()) { // check if the post has a Post Thumbnail assigned to it. 
    the_post_thumbnail(); 
} 
?> 

添加上面的代码中环

然后添加以下代码添加到functions.php

add_theme_support('post-thumbnails'); 

然后在最后,如果你想链接你的缩略图到帖子ID,所以你的帖子点击图片后打开,添加以下代码到functions.php

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

function my_post_image_html($html, $post_id, $post_image_id) { 

    $html = '<a href="' . get_permalink($post_id) . '" title="' . esc_attr(get_post_field('post_title', $post_id)) . '">' . $html . '</a>'; 
    return $html; 

} 

set_post_thumbnail_size(height,width);这是用来增加高度和宽度,在上面的例子中,我添加了50,50。用您所需的值更改它

0

使用新的wordpress版本,您可以从设置>媒体设置缩略图。并将个人尺寸设置为缩略图然后使用此按钮以获得您喜欢的尺寸的缩略图

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