我正在定制一个Wordpress主题,我一直在为"the_excerpt()"添加一个自定义类.
我在网上搜索了这个函数,但没有具体的添加自定义类.
嗯,我已经尝试了"get_the_excerpt",但这个不起作用(什么都不返回).
谁知道怎么做?
小智 10
<?php echo get_the_excerpt(); ?>
Run Code Online (Sandbox Code Playgroud)
显示摘录
<p class="your-class"><?php echo get_the_excerpt(); ?></p>
Run Code Online (Sandbox Code Playgroud)
在带有类的div中显示摘录
小智 7
就我而言,wordpress 没有为此提供解决方案,但是您可以尝试使用以下解决方案打印摘录:
解决方案1:在页面中打印类。
<?php echo '<p class="yourclass">' . get_the_excerpt() . '</p>' ?>;
Run Code Online (Sandbox Code Playgroud)
解决方案 2:覆盖 excert 函数:
您可以使用以下代码覆盖 wordpress functions.php 中的摘录函数并粘贴到同一文件中:
function my_custom_excerpt( $excerpt ) {
$post_excerpt = '<p class="yourclass">' . $post_excerpt . '</p>';
return $post_excerpt;
}
add_filter( 'the_excerpt', 'my_custom_excerpt', 10, 1 );
Run Code Online (Sandbox Code Playgroud)
结论:我更喜欢使用解决方案#1,因为对于维护站点的人来说,它更干净且非常不稳定。