• 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>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

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

    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: https://codex.www.ads-software.com/Function_Reference/the_title

    Thread Starter jbiddulph

    (@jbiddulph)

    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 } ?>

    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘if statement writes out the title twice?!’ is closed to new replies.