Change word displayed by php

Asked

Viewed 78 times

1

I’m picking up a word with a wordpress function, <?php single_cat_title(); ?>, it returns the name of the category to be displayed on the page. It has how to make that when it returned the category with id = 1 or name = name1 it displayed another name?

Example:

On the page of the category "NOME123": <p><?php single_cat_title(); ?></p>, would appear "NOME123"

And on the category page "EMON321" using <p><?php single_cat_title(); ?></p>, cause it to display any other name.

Ta being shown like this: inserir a descrição da imagem aqui

I needed when the word "Time" came up "Team Supply" inserir a descrição da imagem aqui

2 answers

3


Guy doesn’t know much of Wordpress, but the correct way would be you edit it by the administration!

However, if you want to PHP the way you proposed, do the following:

<p><?php 
    $seutitulo = single_cat_title( '', false ); // pega o valor sem imprimir na tela
                                                // Documentação dessa função single_cat_title:
                                                //https://developer.wordpress.org/reference/functions/single_cat_title/

    if(strpos($seutitulo, "Time")!==false)
    {
        $seutitulo = str_replace("Time", "Team Supply", $seutitulo);
    }

    echo $seutitulo;

?></p>    

2

You can create a mapping of the categories, using the name of the category you want to replace as key, and the new name as value:

$categories = array(
  "Time" => "Team Supply"
);

And, when recovering the category, check if it exists in the mapping:

<?php
$cat = single_cat_title("", false);
if (isset($categories[$cat])) {
  $cat = $categories[$cat];
}
?>
<p><?php echo $cat; ?></p>

Notice that the function single_cat_title is receiving two parameters:

  • "" - Category prefix
  • false - Boolean indicating if the function should display the category (true) or just return to category (false).

In this case, we do not want any prefix, and we do not want it to show the category, but to return it, so that we can filter before presenting.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.