WordPress.org

Forums

if statement writes out the title twice?! (5 posts)

  1. jbiddulph
    Member
    Posted 2 years ago #

    hi, I wrote an if statement to change <h2> titles, this sort of works but writes out the titles twice?!

    <?php if(the_title( '<h2>', '</h2>' ) == "Experience") {?>
    				<?php echo '<h2>Profit from our experience</h2>'; ?>
    				<?php } else { ?>
    				<?php the_title( '<h2>', '</h2>' ); ?>
    				<?php } ?>
    				<?php the_content(); ?>

    HTML output:

    <h2>Building, Construction & Interiors</h2>
    <h2>Building, Construction & Interiors</h2>
  2. Andrew
    Forum moderator & snail smusher
    Posted 2 years ago #

    Perhaps the page/post's content too includes the same title.

  3. Ming Sheu
    Member
    Posted 2 years ago #

    You're missing a Parameter on the_title() in the if condition. It's currently echoing the value for display rather than returning the value for comparison.

    the_title( '<h2>', '</h2>', false )

    Reference: http://codex.wordpress.org/Function_Reference/the_title

  4. jbiddulph
    Member
    Posted 2 years ago #

    thanks,

    When may page is on 'Experience' I don't want to show this so I wrote the following code but it doesn't work?!

    <?php if(the_title( '<h2>', '</h2>', false ) == "Experience") {?>
    				<?php echo '<h2>Profit from our experience</h2>'; ?>
    				<?php } else if(the_title( '<h2>', '</h2>', false ) == "Experience") { ?>
    				<?php the_title( '<h2>', '</h2>' ) = ""; ?>
    				<?php } else { ?>
    				<?php the_content(); ?>
    				<?php } ?>
  5. Ming Sheu
    Member
    Posted 2 years ago #

    Sorry, I rushed the response.

    If your title is "Experience" then you just want to compare the title to the string.

    Try the_title( '', '', false ) this will not prepend or append to the title.

    Or, try get_the_title() which should just return the title as a string.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.