Italo Rodrigo
Italo Rodrigo

Reputation: 1785

Get Post title in span tag

i want to get the wordpress title post to a span tag.

i use the code below with the_title() in span tags:

<?php echo '<div>
<img src="image.jpg"/>
<h2><span>'.the_title().'</span>
</h2></div>'; ?>

but the title is show in a tag P, the result is:

<p>TITLE_OF_THE_POST_IS_SHOW_HERE
<div>
<img src="image.jpg"/>
<h2><span></span>
</h2></div>

in result, the span tags is empty, how do insert the title in the span tag?

Upvotes: 0

Views: 462

Answers (1)

Peter
Peter

Reputation: 1290

I would advice changing this

<?php echo '<div>
<img src="image.jpg"/>
<h2><span>'.the_title().'</span>
</h2></div>'; ?>

to something like this

<div>
  <img src="image.jpg"/>
  <h2>
    <?php the_title( '<span>', '</span>' ); ?>
  </h2>
</div>

the php the_title function see documentation Here accepts parameters so you can define the parent tag <span> inside of the wordpress function the_title().

Keep in mind that the_title shows the current post title. If you want to get a title from another post you can use get_the_title( post = 3 ) as Mark B mentioned above.

Upvotes: 0

Related Questions