2011-01-08 34 views
1

我試圖讓我的Wordpress主題爲我在指定爲'圖像'的字段中添加到帖子中的自定義字段中指定的索引頁上列出的每個帖子拉起縮略圖。無論出於什麼原因,get_post_meta()函數都不會返回任何東西,請儘量嘗試。我究竟做錯了什麼?Wordpress - get_post_meta()函數不返回值?

下面的代碼:

<?php while (have_posts()) : the_post(); ?> 


<div class="posts-wrapper"> 
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> 
    <img src="<?php get_post_meta($post->ID, 'image', true); ?>"> 

</div> 

<?php endwhile; ?> 
+0

如果我將代碼更改爲get_post_meta(the_id(),'image',true); - 然後它只輸出帖子ID。例如,我的一個帖子只是輸出鏈接爲「6」。 – Sootah 2011-01-08 08:09:49

回答

1

由於WordPress的2.9,還有一個特色圖像功能,您可以使用縮略圖是多少比使用自定義字段更容易。以下是如何做到這一點:

添加在functions.php中:

if (function_exists('add_theme_support')) { // Added in 2.9 
add_theme_support('post-thumbnails'); 
set_post_thumbnail_size(200, 200, true); // Normal post thumbnails -- values: (width, height, hard-crop-mode); 
add_image_size('home-post-thumbnail', 900, 300, true); // Homepage thumbnail size 
add_image_size('single-post-thumbnail', 300, 9999); // Permalink thumbnail size 
} 

然後你只需要添加這個不管你想要的縮略圖顯示:

<?php the_post_thumbnail('single-post-thumbnail'); // Change according to your thumbnail names ?> 

而且當你寫一個在頁面的最右側,有一個精選圖片部分。選擇你的形象和中提琴! :)

+1

完美無缺! – Sootah 2011-01-08 17:30:58

1

必須從功能回聲返回值get_post_meta()

<img src="<?php echo get_post_meta($post->ID, 'image', true); ?>"> 
+0

試過了,它仍然只輸出帖子ID。 – Sootah 2011-01-08 08:54:30