Display list of taxonomy ('topics') in View

Support MB Views Display list of taxonomy ('topics') in View

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #46550
    sheila@canvas45.com[email protected]
    Participant

    I 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>

    #46557
    PeterPeter
    Moderator

    Hello 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 function get_terms()
    https://developer.wordpress.org/reference/functions/get_terms/

    #46574
    sheila@canvas45.com[email protected]
    Participant

    Thank 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.

    #46583
    PeterPeter
    Moderator

    Hello,

    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

    #46693
    sheila@canvas45.com[email protected]
    Participant

    This 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)

    #46697
    PeterPeter
    Moderator

    You 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 %}
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.