尽管使用了ADD_THEME_SUPPORT(‘后缩略图’),但图像仍未显示;

时间:2013-05-28 作者:user1255049

我有一个帖子图像系统,其中标题中显示了特定类别的帖子缩略图,博客中会出现一个裁剪版本#preview, 以及出现在全文中的全尺寸图像。

这三个位置中没有任何一个出现图像,我也不太清楚原因。

功能。php-

<?php
    register_sidebar();

    if (function_exists(\'add_theme_support\')) {
      add_theme_support(\'post-thumbnails\');
      set_post_thumbnail_size(140,170);
    }

    if ( function_exists( \'add_image_size\' ) ) { 
    add_image_size( \'custom-image\', 440, 265, true ); //(hard cropped)
    }

    function ravs_get_custom_image( $featured_img ){
    global $post, $posts;
    $args = array(
        \'post_type\' => \'attachment\',
        \'numberposts\' => 1,
        \'post_status\' => null,
        \'post_parent\' => $post->ID,
        \'exclude\' => $featured_img
    );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {          
           $img =  wp_get_attachment_image( $attachment->ID, \'custom-image\' );
           return $img;
          }
     }else{
         echo \'Please attach images to your post\';
     }
    }
?>
索引。php

<?php echo get_header(\'cait\');;?>
<?php echo get_sidebar();;?>

<div id="blog">
    <div class="preview">
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <div class="post-image">
            <?php echo ravs_get_custom_image( get_post_thumbnail_id () ); ?>
        </div><!-- end post-image -->
        <div class="post">
            <h2><?php the_title() ;?></h2>
            <span class="post-preview">
                <?php the_excerpt(\'read more...\'); ?>
                <?php endwhile; else: ?>

                    <p>Sorry, no posts to list</p>

                <?php endif; ?>
            </span><!-- end post-preivew -->
            <p class="post-meta"><span style="font-family: amatic;">>></span>&nbsp;Posted on <?php the_date(\'M-d-y\'); ?>&nbsp;<span style="font-family: amatic;">>></span>&nbsp;<?php the_tags(\'tags: \', \', \', \'<br />\'); ?></p><!-- end post-meta -->
        </div><!-- end post -->
    </div><!-- end preview -->
</div><!-- end blog -->


<?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:Pat J 整理而成

由于您似乎正在使用特色图像,请替换此:

<div class="post-image">
    <?php echo ravs_get_custom_image( get_post_thumbnail_id () ); ?>
</div><!-- end post-image -->
使用此选项:

<div class="post-image">
    <?php
        if ( has_post_thumbnail() ) {
            the_post_thumbnail( \'custom-image\' );
        }
    ?>
</div>
参考文献has_post_thumbnail()
the_post_thumbnail()

结束

相关推荐

Hover images and Videos

我用这个代码在我的博客上显示视频循环。它工作得很好。作用phpfunction catch_video() { global $post, $posts; $first_vid = \'\'; ob_start(); ob_end_clean(); $output = preg_match_all(\'/<iframe.+src=[\\\'\"]([^\\\'\"]+)[\\\'\"].*>/i\', $post->post_c