2014-01-06 45 views
0

我試圖用我創建的插件顯示一些文章縮略圖,但是當我將代碼添加到頁面時,它會在頁面開始之前顯示縮略圖我擁有的東西。我不知道那是爲什麼。顯示在頁面開頭的Wordpress post_thumbnail

該插件:

<?php 
add_image_size('cortar', 250, 250, true); // Hard Crop Mode 
function wpb_recent_post() { 
$the_query = new WP_Query('showposts=1&cat=25'); 
while ($the_query -> have_posts()) : $the_query -> the_post(); 
$src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), cortar, false, '');?> 
<img src="<?php echo $src[0];?>" /> 
<?php endwhile; 
} 
add_shortcode('posts_recientes', 'wpb_recent_post'); 
?> 

頁面的內容:

<h3>Poesia</h3> 

[three_columns] 
[column1] 

<h4>This is a heading</h4> 
[posts_recientes] 

[/column1] 
[column2] 

<h4>This is a heading</h4> 
[posts_recientes] 

[/column2] 
[column3] 

<h4>This is a heading</h4> 
[posts_recientes] 

[/column3] 
[/three_columns] 

它顯示方式:

http://www.ixmapp.com/circulodepoesia/prueba/

應該縮略圖之前顯示的標題。

任何幫助將非常感激。

回答

0

Wordpress在加載頁面之前執行一次函數,所以輸出發生在開始時。

嘗試echo完整的img-HTML-tag。

echo '<img src="'.$src[0].'" />'; 
0

謝謝eevaa。你是對的Wordpress在頁面加載之前執行該功能。這是我解決問題的方法。

<?php 
add_image_size('cortar', 250, 250, true); // Hard Crop Mode 

function wpb_recent_post() { 
$the_query = new WP_Query('showposts=1&cat=25'); 
while ($the_query -> have_posts()) : $the_query -> the_post(); 
$src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), cortar, false, ''); 
endwhile; 
wp_reset_postdata(); 
return('<img src=".$src[0]." />'); 
} 
add_shortcode('posts_recientes', 'wpb_recent_post'); 
?>