简体   繁体   English

Wordpress - 循环发布缩略图

[英]Wordpress - post thumbnail in loop

So I'd like to add a thumbnail to my posts but I just can't get it to work.所以我想在我的帖子中添加缩略图,但我无法让它工作。

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

And in my functions.php I've added - add_theme_support('post-thumbnails');在我的 functions.php 中添加了 - add_theme_support('post-thumbnails');

It gives me the option to post the thumbnail when I make a post, but it doesn't show up.它让我可以选择在发帖时发布缩略图,但它没有显示。

What theme or parent theme are you using?您使用的是什么主题或父主题? I usually do something like this inside the loop:我通常在循环中做这样的事情:

<?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

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

Add the above code in loop在循环中添加上面的代码

Then add the following code to functions.php然后在functions.php中添加如下代码

add_theme_support( 'post-thumbnails' ); 

Then at last, if you wish to link your thumbnail to post id, so your posts opens after clicking image, add the following code to functions.php最后,如果您希望将缩略图链接到帖子 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); set_post_thumbnail_size(高度,宽度); this is used to add height and width, in above example i added 50, 50. Change it with your required value这用于添加高度和宽度,在上面的示例中我添加了 50、50。将其更改为您需要的值

with the new wordpress versions you can setup thumbnails from settings > media.使用新的 wordpress 版本,您可以从设置 > 媒体设置缩略图。 and give the personnal size to thumbnail Then use this to get the thumbnail with your prefered size并将个人尺寸提供给缩略图然后使用它来获取您喜欢的尺寸的缩略图

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM