在Wordpress中使用缩略图的自定义字段

时间:2012-02-18 16:32:33

标签: php arrays wordpress

我有一个名为“缩略图”的自定义字段,我目前用它来显示我的Wordpress网站上我的帖子的缩略图。我最近买了一个新模板,似乎无法获得显示此功能的代码。我认为这是因为新模板使用的是数组。

目前,我只需要提出:

<img src="<?php echo get_post_meta($post->ID, Thumbnail, true); ?>"/>

它完美显示。所以这是我认为我需要在新网站中更改的代码:

<?php if($counter <= $big_count): ?>
<?php if($counter == $big_count) { $last = ‘block-item-big-last’; } else { $last = ”; }?>
<div class="block-item-big <?php echo $last; ?>">
<?php if($images && has_post_thumbnail()): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), ‘widget-image’); ?>
<div class="block-image"><a href=’<?php the_permalink(); ?>’ title=’<?php the_title(); ?>’><img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" width=’290′ height=’160′ />

看起来它正在使用数组?但是我对代码的了解不够充分。我试着把我的代码放在上面最后一行的img标签中,但很确定我需要改变别的东西。

请帮忙!感谢。

**更新* *

感谢@ Ehs4n和@Tim的信息!仍然没有运气,但我决定尝试一种更简单的方法,删除检查数组的代码部分。我觉得我很亲密。这就是我现在所拥有的。

<?php if (in_category('movies') ): ?>
<div class="block-item-big-<?php echo $last; ?>">
<div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php echo get_post_meta($post->ID, Thumbnail, true); ?>" alt="<?php the_title(); ?>"/></a><?php echo $icon; ?></div>

但仍然没有缩略图显示?不知道我缺少什么,我有一个名为Thumbnail的自定义字段,其中包含指向该帖子服务器上图片的相应链接。当我将img src中的实际URL放入该图片时,它甚至可以正常工作但是当我输入代码时...

<?php echo get_post_meta($post->ID, Thumbnail, true); ?>

就像它不明白那样。 Firebug告诉我src是空的,就好像它不应该返回缩略图自定义字段一样?

以下是整个文件,它是通过Widget包含在网站索引上显示类别布局。

<?php
add_action('widgets_init', 'pyre_homepage_1col_load_widgets');

function pyre_homepage_1col_load_widgets()
{
    register_widget('Pyre_Homepage_1col_Widget');
}

class Pyre_Homepage_1col_Widget extends WP_Widget {

    function Pyre_Homepage_1col_Widget()
    {
        $widget_ops = array('classname' => 'pyre_homepage_1col', 'description' => 'Homepage 1-column recent posts widget.');

        $control_ops = array('id_base' => 'pyre_homepage_1col-widget');

        $this->WP_Widget('pyre_homepage_1col-widget', 'Avenue Theme: Home 1-column', $widget_ops, $control_ops);
    }

    function widget($args, $instance)
    {
        extract($args);

        $title = $instance['title'];
        $post_type = 'all';
        $categories = $instance['categories'];
        $posts = $instance['posts'];
        $images = true;
        $rating = true;
        $show_excerpt = isset($instance['show_excerpt']) ? 'true' : 'false';

        echo $before_widget;
        ?>

        <?php
        $post_types = get_post_types();
        unset($post_types['page'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);

        if($post_type == 'all') {
            $post_type_array = $post_types;
        } else {
            $post_type_array = $post_type;
        }
        ?>

        <div class="block full">

            <h3><a href="<?php echo get_category_link($categories); ?>"><?php echo $title; ?></a> <span class="arrows">&raquo;</span></h3>

            <?php
            $recent_posts = new WP_Query(array(
                'showposts' => $posts,
                'cat' => $categories,
            ));
            ?>
            <?php
            $big_count = round($posts / 4);
            if(!$big_count) { $big_count = 1; }
            ?>
            <?php $counter = 1; while($recent_posts->have_posts()): $recent_posts->the_post(); ?>
            <?php
            if(has_post_format('video') || has_post_format('audio') || has_post_format('gallery')) {
                $icon = '<span class="' . get_post_format($post->ID) . '-icon"></span>';
            } else {
                $icon = '';
            }
            ?>
            <?php if($counter <= $big_count): ?>
            <?php if($counter == $big_count) { $last = 'block-item-big-last'; } else { $last = ''; }?>

      <!--  Movies Category -->
      <?php if (in_category('movies') ): ?>
      <div class="block-item-big-Movies <?php echo $last; ?>">
                <div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?> movie review'><img src="<?php echo get_post_meta($post->ID, Thumbnail, true); ?>"/></a><?php echo $icon; ?></div>
                <h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?> movie review'><?php the_title(); ?></a></h2>
                <span class="block-meta"><?php the_time('F j, Y'); ?>, <?php comments_popup_link(); ?></span>
                <?php if($show_excerpt == 'true'): ?><p><?php echo string_limit_words(get_the_excerpt(), 15); ?> ...</p><?php endif; ?>
            </div>
       <!-- END Movies Category -->
       <!-- Music Category -->
      <?php elseif (in_category('music') ): ?>
      <div class="block-item-big <?php echo $last; ?>">
                <?php if($images && has_post_thumbnail()): ?>
                <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'widget-image'); ?>
                <div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" width='290' height='160' /></a><?php echo $icon; ?></div>
                <?php else: ?>
                <div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php bloginfo('template_directory'); ?>/images/thumbnail.png&w=290&h=160" alt="<?php the_title(); ?>" width='290' height='160' /></a><?php echo $icon; ?></div>
                <?php endif; ?>
                <h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h2>
                <span class="block-meta"><?php the_time('F j, Y'); ?>, <?php comments_popup_link(); ?></span>
                <?php if($show_excerpt == 'true'): ?><p><?php echo string_limit_words(get_the_excerpt(), 15); ?> ...</p><?php endif; ?>
            </div>
      <!--  END Music Category -->
      <!--  Else Default Display -->
      <?php else: ?>
      <div class="block-item-big <?php echo $last; ?>">
                <?php if($images && has_post_thumbnail()): ?>
                <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'widget-image'); ?>
                <div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" width='290' height='160' /></a><?php echo $icon; ?></div>
                <?php else: ?>
                <div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php bloginfo('template_directory'); ?>/images/thumbnail.png&w=290&h=160" alt="<?php the_title(); ?>" width='290' height='160' /></a><?php echo $icon; ?></div>
                <?php endif; ?>
                <h2>MUSIC<a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h2>
                <span class="block-meta"><?php the_time('F j, Y'); ?>, <?php comments_popup_link(); ?></span>
                <?php if($show_excerpt == 'true'): ?><p><?php echo string_limit_words(get_the_excerpt(), 15); ?> ...</p><?php endif; ?>
            </div>
      <!--  END Normal Display -->
            <?php endif; ?>


            <?php else: ?>
            <div class="block-item-small">
                <?php if($images && has_post_thumbnail()): ?>
                <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'widget-image-thumb'); ?>
                <div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" width='50' height='50' /></a><?php echo $icon; ?></div>
                <?php else: ?>
                <div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php bloginfo('template_directory'); ?>/images/thumbnail.png&w=50&h=50" alt="<?php the_title(); ?>"  width='50' height='50' /></a><?php echo $icon; ?></div>
                <?php endif; ?>
                <h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h2>
                <span class="block-meta"><?php the_time('F j, Y'); ?>, <?php comments_popup_link(); ?></span>
            </div>
            <?php endif; ?>
            <?php $counter++; endwhile; ?>

        </div>

        <?php
        echo $after_widget;
    }

    function update($new_instance, $old_instance)
    {
        $instance = $old_instance;

        $instance['title'] = $new_instance['title'];
        $instance['post_type'] = 'all';
        $instance['categories'] = $new_instance['categories'];
        $instance['posts'] = $new_instance['posts'];
        $instance['show_images'] = true;
        $instance['show_rating'] = true;
        $instance['show_excerpt'] = $new_instance['show_excerpt'];

        return $instance;
    }

    function form($instance)
    {
        $defaults = array('title' => 'Recent Posts', 'post_type' => 'all', 'categories' => 'all', 'posts' => 4, 'show_excerpt' => null);
        $instance = wp_parse_args((array) $instance, $defaults); ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
            <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('categories'); ?>">Filter by Category:</label> 
            <select id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" class="widefat categories" style="width:100%;">
                <option value='all' <?php if ('all' == $instance['categories']) echo 'selected="selected"'; ?>>all categories</option>
                <?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?>
                <?php foreach($categories as $category) { ?>
                <option value='<?php echo $category->term_id; ?>' <?php if ($category->term_id == $instance['categories']) echo 'selected="selected"'; ?>><?php echo $category->cat_name; ?></option>
                <?php } ?>
            </select>
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('posts'); ?>">Number of posts:</label>
            <input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('posts'); ?>" name="<?php echo $this->get_field_name('posts'); ?>" value="<?php echo $instance['posts']; ?>" />
        </p>

        <p>
            <input class="checkbox" type="checkbox" <?php checked($instance['show_excerpt'], 'on'); ?> id="<?php echo $this->get_field_id('show_excerpt'); ?>" name="<?php echo $this->get_field_name('show_excerpt'); ?>" /> 
            <label for="<?php echo $this->get_field_id('show_excerpt'); ?>">Show excerpt</label>
        </p>
    <?php }
}
?>

2 个答案:

答案 0 :(得分:0)

投入print_r($image)以查明Image是否为数组。使用image[0]会产生什么结果?

答案 1 :(得分:0)

如果您使用自定义字段存储缩略图路径,为什么您会对模板中使用的代码感到困惑。

您仍然可以使用旧代码来获取图片

<?php echo get_post_meta($post->ID, Thumbnail, true); ?>

查看模板的代码,它使用Wordpress函数wp_get_attachment_image_src(),它使用三个参数$attachement_id - 传递该媒体的ID,$size - 媒体大小/图像大小以显示和放大;第三个可选$icon,用于显示代表附件的图标。在这里,您将图像集的$ attachment_id作为帖子缩略图在帖子和大小中传递。该函数反过来返回一个包含url,width和height的数组,url是第一个,所以使用$image[0]将返回图像的url,这很好。

如果图片仍然没有显示,您需要确认是否在帖子中附加了图片作为帖子缩略图,$images变量在true子句上返回if