1

I would remove the last character from tag name in tag archive page, I tried:

<?php
echo substr(<?php single_tag_title(); ?>, 0, -1); 
?>

but won't work.

1 Answer 1

2

Two things here, one is wrong syntax because you're opening and closing PHP tags twice. The second problem is the wrong usage of single_tag_title().

If you want this function to return value instead of outputting it (and you need that because you want to manipulate the string), you need to set the second parameter to false. (see the second example: https://codex.wordpress.org/Function_Reference/single_tag_title )

Also it's a good idea to check if you didn't get an empty result.

So your code should look like this:

<?php
$tag_title = single_tag_title( '', false );

if ( ! empty( $tag_title ) ) {
    echo substr( $tag_title, 0, -1 );
}
?>
3
  • Thanks Levi it works now. How can I obtain same thing in search.php? I tried to substutte single_tag_title with the_search_query, but it won't works.
    – Klod
    Commented Aug 22, 2018 at 19:17
  • Try with get_search_query(). It's the same problem - if you use the function, that displays the output (with the echo or print), you won't be able to process is anymore. You need to look for the functions, that return the output without displaying it. In WordPress, there is naming convention that helps with that - the functions that start with the_ (i.e. the_search_query() ) display stuff, and the ones starting with get_ ( get_search_query() ) would only return data, so you can process it and display it yourself. Commented Aug 23, 2018 at 8:25
  • by the way, please accept my answer if it was helpful :) Commented Aug 23, 2018 at 8:26

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.