📜  $trim_length = $custom_field = 'field-name' (1)

📅  最后修改于: 2023-12-03 14:38:44.176000             🧑  作者: Mango

介绍 $trim_length$custom_field 参数

在开发 WordPress 主题时,有时需要在文章列表中截取文章摘要。这时可以使用 get_the_excerpt 函数,但这个函数默认截取的字符数比较少。如果需要截取更多的字符数,我们需要用到 $trim_length 参数。

除此之外,有时还需要自定义摘要的内容,而这个时候可以使用 $custom_field 参数。

$trim_length 参数

$trim_length 参数用于指定截取文章摘要的字符数。默认值是 55,意味着只会截取 55 个字符。如果你需要截取更多字符数,可以在使用 get_the_excerpt 函数时指定 $trim_length 参数,例如:

<?php echo wp_trim_words( get_the_excerpt(), 100 ); ?>

这样会截取文章摘要的前 100 个字符。

$custom_field 参数

$custom_field 参数用于自定义摘要的内容。这个参数需要指定一个自定义字段(Custom Field),并且这个自定义字段需要在文章中编辑过。

例如,在文章编辑页面添加了一个名为 my_excerpt 的自定义字段,那么在主题中就可以这样使用:

<?php echo get_post_meta( get_the_ID(), 'my_excerpt', true ); ?>

这就会输出 my_excerpt 自定义字段中的内容,而不是 get_the_excerpt 生成的摘要内容。

总之, $trim_length$custom_field 这两个参数可以帮助我们更好地控制文章列表中的摘要内容。