【问题标题】:Wordpress - post thumbnail in loopWordpress - 在循环中发布缩略图
【发布时间】:2012-10-18 23:15:10
【问题描述】:

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

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

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

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案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
    
    ?>
    

    【讨论】:

      【解决方案2】:
      <?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。用您需要的值更改它

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 2012-07-14
          • 2017-12-13
          • 1970-01-01
          • 2014-11-13
          • 1970-01-01
          • 2011-09-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多