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

问题描述 投票:0回答:2

我想在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>' ); ?>
php wordpress wordpress-theming
2个回答
3
投票

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>' ); ?>

0
投票

如果您想在项目的所有标题中添加图片,最好使用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);

© www.soinside.com 2019 - 2024. All rights reserved.