GODimm GODimm - 7 months ago 15
PHP Question

Text moves out of Heading Tags

Text within my h2 tags is floating to the top of the division. I feel like there may be something my eyes are overlooking that is causing the title of the post to float to the top of the division. Unfortunatly I cannot see another area where I output the tite.

Here is my HTML/PHP. I appologize for the messyness.

<!-- locations page array -->
<?php $args = array( 'post_type' => 'weblizar_portfolio','posts_per_page' => -1 );
$portfolio = new WP_Query( $args );
if( $portfolio->have_posts() )
{ while ( $portfolio->have_posts() ) : $portfolio->the_post(); ?>
<div class="col-lg-4 col-md-4">
<!--<div class="img-wrapper">-->
<?php $defalt_arg = array('class' => "enigma_img_responsive");
$portfolio_button_link = get_post_permalink( $sample );
$dates = get_post_meta(get_the_ID(), 'date', true);
$postTitle = the_title(); ?>
<?php if(!isMobile()){ ?>
<a href='<?php echo $portfolio_button_link; ?>'>
<div class="author-item">
<div class="imageItem">
<?php if(has_post_thumbnail()):
the_post_thumbnail('portfo_4_thumb', $defalt_arg);
$port_thumbnail_id = get_post_thumbnail_id();
$image_thumbnail_url = wp_get_attachment_url($port_thumbnail_id);
endif; ?>
</div>
<div class="author-info">

<!-- THIS IS THE AREA NOT WORKING -->
<h2><?php echo $postTitle; ?></h2>
<h4><?php echo $dates; ?></h4>
</div>
</div>
</a>
<?php } else { ?>
<table>
<a href='<?php echo $portfolio_button_link;?>'>
<tr>
<td>
<?php echo $postTitle;?>
</td>
<td>
<?php echo $dates; ?>
</td>
</tr>
</a>
</table>
<?php } ?>


And here is the executed html:

<div class="col-lg-4 col-md-4">
<!--<div class="img-wrapper">-->
San Jose <a href="">
<div class="author-item">
<div class="imageItem">
<img width="260" height="160" src="" class="enigma_img_responsive wp-post-image" alt="San Antonio" srcset="" sizes="(max-width: 260px) 100vw, 260px"> </div>
<div class="author-info">
<h2></h2>
<h4>April 23rd, 2016</h4>
</div>
</div>
</a>

<!--</div>-->
</div>

Answer

You should replace the_title with get_the_title. The reason is that the_title just echos the title wherever you use it, while get_the_title returns it (which is what you want).

Edit:

Another solution is to call the_title() where are displaying your h2 tag like this:

 <h2><?php the_title() ?></h2>