admin管理员组

文章数量:1122832

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?

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?

Share Improve this question asked Apr 13, 2020 at 14:30 SeryozhaSeryozha 191 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: tagsDisplay text if title in archive has specific word