在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():
这个函数用于输出文章的完整内容。