Forum Moderators: coopster

Message Too Old, No Replies

replace specific string, else otherwise

         

smallcompany

8:08 pm on Feb 3, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,

In WP theme file I have this:

<h1 class="entry-title"><?php the_title(); ?></h1>

In a single instance, I need to replace the specific string, let it be "Wrong Title"

All else should be as it is, that is, the_title() should be let do the job.

I tried this:

<?php if(the_title() == 'Wrong Title') 
{
echo $this->__('Good Title');
}
else
{
the_title();
}
?>


and the wrong string did not get replaced, plus all titles showed up twice.

So, why wrong title did not get replaced, and why all got printed twice?

Thank you

phparion

7:37 pm on Feb 5, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



show your full code. wordpress themes can get funny sometimes. btw, will it not be easy to just change the wrong title in the post/page itself in the admin panel rather than doing it in programming? I am not sure what do you want to achieve from it?

smallcompany

8:10 pm on Feb 5, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Uh, first thanks for mentioning how sometimes WP themes behave.
It's a plugin that does a good job, but is little hardcoded... so no way to input the title for one specific page in an easy way. The rest works fine.
Anyway, it turned that the_title() requires some sort of loop (per WP docs), and get_the_title() should be used otherwise (WP thing). When I replaced that in my code, it worked.

Now it looks like this (full code):

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php } elseif (get_the_title()=="Wrong Title") { ?>
<h1 class="entry-title"><?php echo 'Good Title'; ?></h1>
<?php } else { (get_the_title()!= "Wrong Title") ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php } ?>


Don't know if this could be improved, my measure was that the pages show right titles.

Thank you

phparion

6:48 am on Feb 6, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It certainly can be improved, depending on how many titles we are talking about? if you have a small blog where you can hardcode IF ELSE to check your titles, then hmm, OK, but if it is going to be a content rich wp installation then you will end up writing unending IF ELSE statements. Can't you fix the titles from PERMALINK in wp admin panel? I mean what do you want to achieve really? you want to add some custom word/phrase for SEO in links but do not want them to be the part of post titles or you are trying to integrate wp in your current website design and want to make things look equal? I integrated wp in my website at [fastcreators.com ] and it works fine. In fact, on the front page of the site I use REST API to show my latest post from my wp on my website that is based on a different CMS. I also have a free wp theme, which is very simple and if you download it and check the header.php you will see how I did the title thingy. My idea from this theme was to give out something that can be easily integrated with your current website, design-wise.