如何在 Wordpress 内置 the_title() 中显示图像?

How can I display images in Wordpress inbuilt the_title()?

我想在 Wordpress 内置函数 the_title() 中显示图像。请帮忙。 我的代码:

<?php the_title( '<h3 class="entry-title"><img src="<?php echo (get_template_directory_uri()); ?>/images/line.png">','<img src="<?php echo (get_template_directory_uri()); ?>/images/line.png"></h3>' ); ?>

the_title() 是这样工作的:

<?php the_title( $before, $after, $echo ); ?> 

默认为 $echo = true ,所以只需传递 $before$after

像这样使用:

<?php the_title( '<h3 class="entry-title"><img src="'.get_template_directory_uri().'/images/line.png">','</h3>' ); ?>

如果标题前后都需要图片,请使用以下代码:

<?php the_title( '<h3 class="entry-title"><img src="'.get_template_directory_uri().'/images/line.png">','<img src="'.get_template_directory_uri().'/images/line.png"></h3>' ); ?>

如果你想在项目的所有标题中添加图像,最好给我们the_title过滤器 (在给定的代码中,get_template_directory_uri() 用于 parent 主题,如果您在 child 主题中工作,则必须将其更改为 "get_stylesheet_directory_uri()")

function add_image( $title) {

$src = get_template_directory_uri().'/assests/images/header.jpg';

return $title.'<img src=" '.$src.' ">';

} add_filter( 'the_title', 'add_image', 10);