PHP if/else structure in WordPress template file doesn’t work

Question about a piece of PHP code in a WordPress template file.

The template contains this code:

Read More
<h1><?php the_title(); ?></h1>

I want the title only be printed if the title is not “Home”.

But this code doesn’t work:

<?php if (the_title()!='Home'); ?>
   <h1><?php the_title(); ?></h1>
<?php endif; ?>

Related posts

Leave a Reply

4 comments

  1. the_title() echoes, it doesn’t return its title.

    Use get_the_title() instead.

    <?php if (get_the_title() != 'Home'): ?>
       <h1><?php the_title(); ?></h1>
    <?php endif; ?>
    

    As an aside, it looks like you’re trying to detect if you’re on the home page. Checking against a title can be flaky, as that can change.

    Use is_home() instead.

    <?php if ( ! is_home()): ?>
       <h1><?php the_title(); ?></h1>
    <?php endif; ?>