如何在WordPress中獲取特色圖片URL(PHP程式碼片段)

如何在WordPress中獲取特色圖片URL

如果您正在建立一個 WordPress 主題、外掛或只是一個自定義函式,那麼在 WordPress 中返回特色圖片將非常有用。無論是用特色圖片顯示最近的文章,還是在主題的 single.php 中顯示特色圖片,這些函式都能為你所用。

重要的是,如果你想在主題中啟用文章縮圖功能,就必須在網站的 functions.php 中新增以下內容。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//Enable theme support for featured images
add_theme_support('post-thumbnails');
<?php //Enable theme support for featured images add_theme_support('post-thumbnails');
<?php
//Enable theme support for featured images
add_theme_support('post-thumbnails');

大多數主題已經啟用了這一功能,但如果您要從頭開始建立一個主題,則需要新增主題對文章縮圖的支援。如果你在 WordPress 編輯器中看到特色圖片選項,這意味著它已經啟用。

古騰堡編輯器中的特色圖片選項

古騰堡編輯器中的特色圖片選項

如何在 PHP 中顯示特色圖片

使用 WordPress 內建函式 get_the_post_thumbnail() ,在 <img> 標籤中顯示文章的特色圖片。這是在 WordPress 迴圈中顯示文章特色圖片的最簡單方法。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//Displays the featured image in a <img> tag (use this in a loop)
echo get_the_post_thumbnail();
<?php //Displays the featured image in a <img> tag (use this in a loop) echo get_the_post_thumbnail();
<?php
//Displays the featured image in a <img> tag (use this in a loop)
echo get_the_post_thumbnail();

此外,如果您想為特色圖片獲取特定尺寸,可以在第二個引數中填寫圖片尺寸

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//Displays the featured image in a <img> tag resized to the 'large' thumbnail size (use this in a loop)
echo get_the_post_thumbnail( get_the_ID(), 'large' );
<?php //Displays the featured image in a <img> tag resized to the 'large' thumbnail size (use this in a loop) echo get_the_post_thumbnail( get_the_ID(), 'large' );
<?php
//Displays the featured image in a <img> tag resized to the 'large' thumbnail size (use this in a loop)
echo get_the_post_thumbnail( get_the_ID(), 'large' );

有時,您需要使用文章特色圖片的實際 URL,這將在下一步中介紹。

如何用 PHP 獲取 WordPress 文章特色圖片的 URL

如果您使用的是 WordPress 4.4 以上版本(2015 年釋出),您可以使用 get_the_post_thumbnail_url() 函式返回特色文章圖片的URL。如果您想在背景圖片樣式中使用特色圖片的 URL,或製作一個特別需要特色圖片 URL 的獨特主題元素,這將非常有用。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//Display the featured post URL (you can replace 'medium' with a different image size)
echo get_the_post_thumbnail_url( get_the_ID(), 'medium' );
<?php //Display the featured post URL (you can replace 'medium' with a different image size) echo get_the_post_thumbnail_url( get_the_ID(), 'medium' );
<?php
//Display the featured post URL (you can replace 'medium' with a different image size)
echo get_the_post_thumbnail_url( get_the_ID(), 'medium' );

上述程式碼段還可以自定義不同的圖片尺寸,包括 WordPress 的預設值縮圖、中圖、中大圖、大圖和全圖。

如何在 PHP 中獲取特色圖片 ID

使用 get_post_thumbnail_id() 函式可以獲取文章特色圖片的媒體 ID。這對於在 WordPress 中將媒體 ID 用於其他功能非常有用。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
// Get the ID of the featured image
echo get_the_post_thumbnail( get_the_ID(), 'large' );
<?php // Get the ID of the featured image echo get_the_post_thumbnail( get_the_ID(), 'large' );
<?php
// Get the ID of the featured image
echo get_the_post_thumbnail( get_the_ID(), 'large' );

希望這對您在 WordPress 網站上獲取特色圖片有所幫助。如果您有任何問題,請在下面的評論中告訴我們!

評論留言