0

In tag pages, I want to display specific text if the title containing a specific word.

As example:

I have a Tag and tag archive page: Yellow Banana

And I want to display “cool fruits” in all tag pages that have in title word “Banana”.

Can you tell me the code?

1 Answer 1

0

You may use string comparison. I could illustrate a simple example code.

It is assumed that the user with the following knowledge/experience

  • PHP - understand what is variables, static value
  • WordPress templates and how to modify them

There are many ways to test string such as

  • php string comparison strpos() (simple)
  • php regular expression preg_match() (required more test and knowledge)

This example shows the simple way to check the title.

Assumed you are talking about templates so the following code will be put in tag archive template. eg. tag.php

<?php 
// assumed you are doing it in a loop
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        $str_search = strpos( $post->title, 'Banna' ); // assumed when your $post-title contains 'Banna'

        if( $str_search !== false && $str_search >= 0 ) {
            echo 'cool fruits';
        }

    endwhile;
endif;

?>
  • A friendly reminder: make sure you read, research and test rather than just copy and paste and hope this answer resolve all the questions and troubles instantly without spending further effort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.