0
votes

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?

1
get_the_title(), probably, as per usual with wordpress. the_foo() outputs data, get_the_foo() returns the dataMarc B
thanks, its works ^^ and, how i get the image post url? get_the_thumbnail?Italo Rodrigo
i get the url image: $i = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium' ); i use: <img src="'.$i[0].'"/> its works, but is include a br tag in the end. how i elimine the br tag?Italo Rodrigo

1 Answers

0
votes

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.