Robin Cox
Robin Cox

Reputation: 1490

Wordpress, use tag slug to find tag name and store it in a variable

By doing this:

if (has_tag( "my-tag-slug", $post )) { $postsTag = "my-tag-slug"; $tagImageFormat = ".jpg"; }

I get:

- a variable named $postsTag containing the string "my-tag-slug"

- a variable named $tagImageFormat containing the string ".jpg"

The name of the tag that has this slug is "My tag slug". How can I get the following:

- a variable named $postsTagName containing the string "My tag slug"

By using the slug I grabbed from the function?

Upvotes: 0

Views: 457

Answers (1)

Bud Damyanov
Bud Damyanov

Reputation: 31889

Yes, you can use the slug, grabbed from the function, because directly you cannot "un-slug" the string with Wordpress build-in functions, actually what you see as a slug, is the post_name, stored inside the database (Wordpress internally sanitize the titles of all saved posts / pages / attachments / etc with sanitize_title() function).

However, you can use simple PHP string string manipulation and construct your $postsTagName variable as you want, for example:

<?php    
$postsTagName = str_replace('-', ' ', ucfirst(strtolower('my-tag-slug')));
//Now your variable will contain My tag slug
?>

The PHP FIDDLE is here.

Notes: The above code simply replace the dashes - from the string and capitalize the first character.

Upvotes: 1

Related Questions