Display list of taxonomy ('topics') in View
- This topic has 5 replies, 2 voices, and was last updated 7 months ago by
Peter.
-
AuthorPosts
-
September 27, 2024 at 7:11 PM #46550
[email protected]
ParticipantI created a cpt with a special taxonomy (called 'topics') and need to display each topic in a sidebar div with a link to that taxonomy page. I am using Views and cannot figure out how to output the full list of 'topics'. Can you help?
For reference, my code looks like this, but it only displays 1 'topic' instead of the entire list of 'topics':
</div>
<div class="sidebar">
<div class="recent-posts">
<h3>Topics</h3>
<p>{{ mb.get_the_term_list( post.ID,'topic', '', ', ' ) }}</p>
</div>
</div>
</div>September 28, 2024 at 7:13 AM #46557Peter
ModeratorHello Sheila,
The code
{{ mb.get_the_term_list( post.ID,'topic', '', ', ' ) }}
will get the term list of the current post or a specific post so I think you see only one term of the post.If you want to show all terms of the taxonomy
topic
, you can use the WordPress functionget_terms()
https://developer.wordpress.org/reference/functions/get_terms/September 30, 2024 at 11:31 PM #46574[email protected]
ParticipantThank you. I used your answer to construct this line in the Views screen:
Replaced this: {{ mb.get_the_term_list( post.ID,'topic', '', ', ' ) }}
With this: {{ mb.get_terms('topic') }}Unfortunately that just outputs the word "ARRAY" instead of the desired taxomony which is 'topic'. Alternatively, I tried using topics (plural) but it gave the same result.
October 1, 2024 at 11:03 PM #46583Peter
ModeratorHello,
The WordPress function get_terms() returns an array of terms. So you need to use a loop to output the term in the array. Here is an example:
{% set cats = mb.get_terms({ 'taxonomy': 'topic', 'hide_empty': false }) %} {% for cat in cats %} {{ cat.name }} <br> {% endfor %}
You can read more about how to use PHP functions in the View template: https://docs.metabox.io/extensions/mb-views/#running-php-functions
October 16, 2024 at 3:38 AM #46693[email protected]
ParticipantThis is great and it successfully output the list of all available special taxonomy 'topics'. Is there a snippet of code that I can add that will actually make them a link to that particular 'topics' page? For example, I have a 'topic' named 'careers' - I need the output 'career' to be a link to the corresponding webpage (i.e. https://mydomain.com/topic/career)
October 16, 2024 at 8:51 PM #46697Peter
ModeratorYou can get other term info by using the WordPress function. To get term link, try to use the function
get_term_link()
https://developer.wordpress.org/reference/functions/get_term_link/{% for cat in cats %} <a href="{{ mb.get_term_link( cat.term_id ) }}">{{ cat.name }}</a> <br> {% endfor %}
-
AuthorPosts
- You must be logged in to reply to this topic.