我想在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()用于父主题,如果您在子主题中工作,则必须使用“ 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);