TransWikia.com

Show all Tags in each post

WordPress Development Asked by Hoàng Năng Hưng on December 31, 2021

I have created a new post type named “Project”. And I registered 1 taxonomy “Tags” in it as follows:

https://pastecode.xyz/view/844258b1

I entered the tags into 1 article in post type “Project”.

If you want to enter an article, it will display all the tags in that article.

Can someone help me. Thank you!

3 Answers

Considering you mention having registered a custom taxonomy instead of using the native tags, the functions the_tags() and get_tags will not work.

you need to get your custom taxonomy terms by using get_the terms(). Usage is not much different from using native tags, but you have to pass the post_type as a variable. See the Wordpress codex on this: https://developer.wordpress.org/reference/functions/get_the_terms/

Answered by Ralph Smit on December 31, 2021

Check out get_the_tag_list() to retrieve all the tags:

https://codex.wordpress.org/Function_Reference/get_the_tag_list

Examples:

$separate_meta = __( ', ' );
echo $tags_list = get_the_tag_list( '', $separate_meta );

OR:

<?php
 $tags = get_tags();
 if ($tags) {?>
  <ul class="tags">
  <?php
    foreach ($tags as $tag) {
    echo '<li><a href="' . get_tag_link( $tag->term_id ) . '" 
    title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a></li>';
  }
  ?>
  </ul>
<?php }?>

Answered by Rizwan Zakir on December 31, 2021

You have to be careful when creating a custom post type with taxonomy and the same namespace "tags", since tags are already registered by WordPress default for the post type posts. Maybe the renaming of the taxonomy to project-tags or something else would make sense. Otherwise the taxonomy will be mixed up by WordPress.

To show all tags you can use PHP:

the_tags('<ul class="taglist"><li>', '</li><li>', '</li></ul>');

Note: This function must be within the loop (archive.php or single.php).

Answered by user3135691 on December 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP