1

I am trying to get the tags from the post (there will only be one tag), then create a new variable with that tag name but with the spaces replaced with dashes.

This is what a have so far, but I cannot get it to display the value of the tag (both with spaces and without) instead of "Array". In the code below, I'm only trying to get it to display with the dashes right now.

        $posttags = get_the_tags();
        $newname = str_replace(' ', '-', $posttags);
        if ($posttags) {
          foreach ($posttags as $tag){
            echo 'Post by: <a href="/meet-the-team/employees/'. $newname .'/">'. $newname .'</a>';
          }
        }
  • get_the_tags will return an array even if there's only a single tag, aka a list of 1 is still a list, in cases like these you need to break down each step and check your assumptions ( e.g. that $posttags is a string, not an array. In fact it would be a WP_Term object, not a string ) – Tom J Nowell Oct 19 '17 at 20:49
  • I figured it out.. I was on the right track but I didnt realize I needed to specific the $tag->name inside of the str_replace... $posttags = get_the_tags(); if ($posttags) { foreach ($posttags as $tag){ $newname = strtolower(str_replace(' ', '-', $tag->name)); echo 'Post by: <a href="/meet-the-team/employees/'.$newname.'">'. $tag->name .'</a>'; } } – sk03 Oct 19 '17 at 20:52
3

Lets break this apart using your question title:

Get the tags as an array, convert the tag into a string, and then replace spaces with dash

Get the tags as an array

get_the_tags is the correct function, you now have one of 2 things:

  • an array of term objects ( a tag is a term in the tags taxonomy )
  • false, there are no tags on this post
  • A WP_Error object, something went wrong

So we need to check for those cases, afterall if we assume tags are returned but there are none, what will happen? We have to tell it what will happen or undefined things will happen, and that could be anything, probably a 500 error

So lets update the first bit of code:

$tags = get_the_tags();

if ( is_wp_error( $tags ) ) {
    return; // there was an error
}
if ( false === $tags ) {
    return; // there were no tags on this post
}
// now we can do stuff with the tags

Convert The Tag into a string

Nifty, now we have an array of tags. You mentioned there should only be 1 tag, but who knows what plugins might be adding their own tags, so lets make sure your code handles that correctly:

foreach ( $tags as $tag ) {
    // do something with our tag
    break; // exit after the first one
}

Replace the spaces with dashes

Each $tag is a WP_Term object, you can use the terms slug with $tag->name, which will already have the URL friendly version

However, why bother? There's a function for that! We can replace this:

echo 'Post by: <a href="/meet-the-team/employees/'. $newname .'/">'. $newname .'</a>';

With:

$url = get_term_link( $tag, 'tags' );
echo 'Post by: <a href="'.esc_url( $url ).'">'.esc_html( $tag->name ).'</a>';

Notice I added escaping. Escaping is important and will prevent most hacks. Despite it's anti-hacking superpowers, it's almost never applied to WordPress code :(

  • Only thing missing is a "so to wrap it all up," but, on the other hand, this isn't Smashing or Tuts, where I think you'd need a download link, too - so: Very nice! – CK MacLeod Oct 19 '17 at 21:53
  • @CKMacLeod problem with that is it gives everyone copypasta, and nobody learns anything. Then the requirements change in a trivial way and they have no idea what to do and come back asking you to update a word, e.g. "it says Post by: but I need it to say Page by:, can you change the code for me?". I'm sure sk03 doesn't need me to pre-assemble it all though :) Though a further reading section is always nice to have – Tom J Nowell Oct 19 '17 at 23:25
1

If you use str_replace on an array, you get an array back. So $newname is rightly an array.

You may want to do the str_replace inside your foreach loop instead - that way it works on each individual item and then you display the individual item.

Or, if you keep using str_replace on the full array, you need to change your foreach loop to: foreach($newname as $tag) and echo $tag not $newname.

  • 1
    get_the_tags() returns an array of tag objects, not tag names. So, I'll answer below, I guess. – CK MacLeod Oct 19 '17 at 20:48
  • 1
    Thanks guys, I'll remove my question. I figured it out. Just needed to put $tag->name inside of str_replace instead of just $tag. – sk03 Oct 19 '17 at 20:52
  • @sk03 don't remove your question, then nobody would learn, besides, there are other things you need to do that you've missed – Tom J Nowell Oct 19 '17 at 20:54
  • @tomjnowell I agree with keeping it, but in similar situations in the past, people who commented on my questions were pretty rude about deleting the question once it's solved (either quickly or by myself). Those were on the main stackoverflow site, not the WP site so I'm glad you guys are understanding that stuff like this does help people learn. Maybe upvote the question or my comment/answer? I'd really appreciate it – sk03 Oct 19 '17 at 20:57
  • @sk03 you can always answer your own question, you even get a badge on your profile for doing it :) Haters gonna hate, if they're nasty about it flag the comment but don't feel the need to delete a question – Tom J Nowell Oct 19 '17 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.