Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking to see if it's possible to find and remove duplicates from the arrays generated below. I've tried array_unique array_merge and they haven't worked.

$category = get_the_category();
$tags = wp_get_post_tags($post->ID);

As an example, there is a potential that the word "Apparel" may appear in both Category and Tag for the post. With the "raw" method above, Apparel would appear twice in my output. I'm only looking to include it once. At this point in time, I believe it is ok if both lists are merged and all words are part of 1 output.

Example: %23Apparel %23Auto %23Mens

Then, once the output is generated, I need to add HTML URLEncode %23 to the beginning of each word.

Would appreciate your input.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

If you only want to collect the category+tag names into the output, you can try this

<?php 
global $post;
$words=array();

$tags = wp_get_post_tags($post->ID);
foreach($tags as $tag){
    $words[]="%23".$tag->name;
}

$cats = get_the_category($post->ID);
foreach($cats as $cat){
    $words[]="%23".$cat->name;
}

$words=array_unique($words);
echo implode(" ",$words);
?>
share|improve this answer
    
Worked great except for one thing. The implode ignores the first word in the string. Is there a way to have the %23 apply to all? –  Pat Mar 12 '13 at 12:22
    
yes sure, you can do this echo "%23".implode(" %23",$words); in the previous revision of the answer or use the updated answer. –  birgire Mar 12 '13 at 12:31
    
The revised answer works great. I was trying to take your original code and add a foreach onto the $words array and was failing. Thank you! –  Pat Mar 12 '13 at 12:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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