0

I want to show plain text tag names above each post on a page that displays posts for a given category. Each post only has one tag.

I've set up an array to grab posts from the category I want and made my WP_Query query. The posts display in nice div's, with their titles, content, etc.. My problem is those darn tags!

I can display each post's tag name using this:

echo the_tags('');

But this serves up

<a href="[link to tag page]" rel="tag">[My Tag Name]</a>

I've tried variations of strip_tags and several functions in the Wordpress Codex on the_tags() to remove the html - all to no avail.

I am able to strip out the html using the next method (displaying plain text, hooray!), but unfortunately every post adds tag names to the short array $term_names[] and the list of tags displayed next to each post grows with every post.

$terms = wp_get_post_tags( $post->ID );
foreach( $terms as $term )
$term_names[] = $term->name;
echo implode( ', ', $term_names );

Is there something I am missing? Clearly there must be.

  • the_tags() echos, not returns. You can do get_the_tags() instead codex.wordpress.org/Function_Reference/get_the_tags — also you’re not defining/resetting $term_names , that’s why it keeps growing. Do $term_names = array(); before the foreach loop – David Sword Jan 31 '18 at 2:40
  • Gah! Thank you! I have been going crazy wondering what basic thing I was missing. I spent way too much time on this today. I added $term_names = array(); and everything works now. echo get_the_tags('') was producing the same output I was having problems with, FWIW. THANK YOU! – ScragglyCouch Jan 31 '18 at 2:53
1

Well, you can use the DOM object to change/remove tags and their parameters. See here for all the details https://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element

Code would be something like this: (this sample changes the class of all H1 elements to be 'a_new_class', removing any existing classes)

$html = "<h1 class='the_old_class'>A Heading</h1>";
$dom = new DOMDocument;
libxml_use_internal_errors(false); // supress errors
$dom->loadHTML($html, LIBXML_NOERROR);  // supress errors
foreach ($dom->getElementsByTagName('h1') as $node) {
    $node->setattribute('class','a_new_class');         
    $dom->saveHtml($node) ;
}
$html = $dom->saveHTML();   // saves the object (all of the html) so we can return it

Although the other answer works; I add this to show an alternative method of changing elements that doesn't rely on regex trickery.

  • nice alternative to the usual usage of str_replace and its like, but seems like a little overwhelming in this case ;) – Mark Kaplun Jan 31 '18 at 6:04
  • True, Mark. Just put it here for future searchers. Worked real well on very large documents that I needed to process. Thanks. – Rick Hellewell Jan 31 '18 at 6:49

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.