0

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

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

当我发帖时,它让我可以选择发布缩略图,但它没有显示出来。

4

3 回答 3

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

?>
于 2012-10-19T07:50:28.617 回答
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(高度,宽度);这用于添加高度和宽度,在上面的示例中我添加了 50、50。用您需要的值更改它

于 2012-10-21T17:19:35.210 回答
0

使用新的 wordpress 版本,您可以从 settings > media 设置缩略图。并为缩略图提供个人尺寸然后使用它来获取具有您喜欢的尺寸的缩略图

<?php the_post_thumbnail('thumbnail');?>
于 2013-12-22T03:15:07.693 回答