2017-09-29 64 views
0

下面的代碼是很好的獲取信息網址和標題:如何獲取與WordPress博客精選圖片相關的圖片文字?

global $post; 
$args = array('posts_per_page' => 5, 'offset'=> 1, 'category' => 4); 

$myposts = get_posts($args); 
foreach ($myposts as $post) : setup_postdata($post); 
    ?> 
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <br /> 
    <?php 
endforeach; 
wp_reset_postdata(); 

但是我需要的是獲得功能圖像包括路徑的文/ url,但不是打印到屏幕上。這樣做的目的是讓我可以使用ImageMagick轉換創建自定義大小的圖像,並將其顯示到屏幕上而不是原始圖像。

我會做這樣的事情

convert "$image" -resized (my custom size) -strip 400x/$image 

轉換代碼是更復雜一點,因爲我打算建立大型廣場豎起大拇指,但這是我們的目標。然後,我會將這些圖像發佈到一個頁面上的方形豎框畫廊以及各個帖子的鏈接中。

有沒有像the_featured_image()這樣的東西能讓我看到圖像,然後我可以將它設置爲一個變量並運行轉換代碼?

+0

你的意思後縮略圖? the_post_thumbnail() –

回答

1

您可以使用get_the_post_thumbnail_url來獲取特色圖片的網址,如果沒有網址,可以使用false。 所以你foreach循環內

$thumb_url = get_the_post_thumbnail_url(get_the_ID(), 'full'); //can also be $post->ID 
if($thumb_url) 
{ 
    //do your stuff 
} 
+0

這很好,有用。我將如何採取the_title();並把它變成像$ the_title這樣的變量,然後按照我的意願去做,而不是打印到屏幕上? –

+0

@BobM'get_the_title()'或'$ post-> post_title' –

+0

好極了,讓生活再次變得美好。 –

相關問題