Change word displayed by php [closed]

1

I'm getting a word with a wordpress function, <?php single_cat_title(); ?> , it returns me the name of the category to be displayed on the page. Can you do that when it returns the category with id = 1 or name = name1 it displays another name?

Example:

In the category page "NAME123": <p><?php single_cat_title(); ?></p> , "NAME123" would appear

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

It looks like this:

Ineededthatwhenitwastheword"Time", it would appear "Team Supply"

    
asked by anonymous 07.12.2016 / 19:14

2 answers

3

Dude does not handle too much of Wordpress, but the correct way would be for you to edit this by admin!

However if you want to mess with 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>    
    
07.12.2016 / 19:49
2

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

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

And, when retrieving 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>

Note that the function single_cat_title is getting two parameters:

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

In this case, we do not want a prefix, and we do not want it to present the category, but rather to return it, so we can filter before submitting.

    
07.12.2016 / 19:50