Wordpress - 在循环中发布缩略图

时间:2012-10-18 23:15:10

标签: php wordpress wordpress-theming

所以我想在我的帖子中添加一个缩略图,但我无法让它发挥作用。

<?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');

它让我可以选择在发帖时发布缩略图,但它不会显示。

3 个答案:

答案 0 :(得分: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 :(得分: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。用你需要的值改变它

答案 2 :(得分:0)

使用新的wordpress版本,您可以从设置&gt;设置缩略图媒体。并将个人大小赋予缩略图然后使用此缩略图获取您喜欢的大小的缩略图

<?php the_post_thumbnail('thumbnail');?>
相关问题