[英]WordPress get author name for singular page/post
我想顯示“作者元標記”的作者姓名,我使用下面的代碼來做到這一點,但是我總是得到一個空字符串:
$fname = get_the_author_meta('first_name');
$lname = get_the_author_meta('last_name');
$author = trim( "$fname $lname" );
if ( $author ) { ?>
<meta name="author" content="<?php echo $author; ?>">
<?php } ?>
如何獲得當前顯示的頁面/帖子作者姓名?
謝謝
您需要首先檢查您是否從get_the_author_meta()獲得了正確的值。 因此,您將知道問題是來自修整/條件部分還是來自wordpress本身。 為此,請添加您的代碼:
echo "Here is my result : ".get_the_author_meta('first_name')." ".get_the_author_meta('last_name');
一旦測試完成,我確定您需要編輯您的問題。
將此答案作為調試的一般建議,而不是針對您的問題的解決方案。
好的,我知道了。 作者的名稱實際上是由php在您的HTML頁面中顯示的,但是它封裝在meta標記中,該標記僅在頁面的頭部使用,並且不會為用戶產生任何可見的輸出。
嘗試使用div標記而不是meta標記,並確保您在頁面的主體部分內書寫。
$fname = get_the_author_meta('first_name');
$lname = get_the_author_meta('last_name');
$author = trim( "$fname $lname" );
if ( $author ) { ?>
<div>Author: <?php echo $author; ?></div>
<?php } ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.