• Resolved piblondin

    (@piblondin)


    Hi everyone,

    I am trying to get my post tags as a php variable. What I want is to have them be something like this: $posttags = tag1&tag2&tag3&tag4

    I need the ampersands between the tags because I am putting them into a Google search query URL. I’m able to echo the string that I want using the code below, but how do I store it as a variable? Thanks for any help you can provide!

    $posttags = get_the_tags();
    if ($posttags) {
    foreach($posttags as $tag) {
    echo $tag->name . '&'; } }

Viewing 2 replies - 1 through 2 (of 2 total)
  • $posttags = get_the_tags();
    if ($posttags) {
    foreach($posttags as $tag) {
    $tags=$tags. $tag->name . '&'; }
    $posttags=substr($tags,0,-1);
    }

    You could also build an array and join the values together using implode ….

    $string = array();
    $tags = get_the_tags();
    if( $tags ) {
    	foreach( $tags as $tag ) {
    		$string[] = $tag->name;
    	}
    	//echo implode( '&' , $string ); // Echo string
    	//$my_var = implode( '&' , $string ); // Store in var
    }

    Just to throw another approach out there…

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to get post tags as a PHP variable’ is closed to new replies.