在WordPress中,如果你想實現“如果有摘要就調用摘要,如果沒有摘要就調用全文”的功能,可以使用以下代碼:
<?php if (has_excerpt()) {
echo '<p>';
echo $description = get_the_excerpt();
echo '</p>';
echo '<p><a href="' . get_permalink() . '" class="btn btn-outline-customer">Read more <i class="ti-arrow-right"></i></a></p>';
} else {
the_content('Read the rest of this entry »');
} ?>
代碼解釋:
has_excerpt():
這個函數用于檢查當前文章是否有手動設置的摘要。
如果有摘要,返回 true;如果沒有摘要,返回 false。
the_excerpt():
這個函數用于輸出文章的摘要。
如果文章沒有手動設置摘要,WordPress會自動截取文章的前55個單詞作為摘要。
the_content():
這個函數用于輸出文章的完整內容。