• We have a Custom Post (‘Event’) that has a custom taxonomy/category (‘Venue’) – used for selecting all the events at a Venue.
    How do we display the category on the post?
    [In Admin – it is displayed in the right-hand pane. We need to display it also on the Frontend, formatted nicely]
    It’s 2024 – I had expected to be able to write pseudo-code e.g. “Variable Name: $Variable(s)
    So far all I’ve found is the need to write bespoke PHP (see example below). Is there a better/simpler way?

    /* see here: https://wordpress.stackexchange.com/questions/130993/list-taxonomy-terms-in-current-post-current-category/
    *
    * List the taxonomies and terms for a given post
    *
    * @param int $post_id
    * @return string
    */
    function get_the_current_tax_terms_wpse( $post_id )
    {
    // get taxonomies for the current post type
    $taxonomies = get_object_taxonomies( get_post_type( $post_id ) );

    $html = "";
    foreach ( (array) $taxonomies as $taxonomy)
    {
    // get the terms related to the post
    $terms = get_the_terms( $post->ID, $taxonomy );
    if ( !empty( $terms ) )
    {
    $li = '';
    foreach ( $terms as $term )
    $li .= sprintf( '<li><a href="%s">%s</a></li>',
    get_term_link( $term->slug, $taxonomy ),
    $term->name );

    if( ! empty ( $li ) )
    $html .= sprintf( '<li><ul><li>%s:</li>%s</ul></li>',
    $taxonomy,
    $li );
    }
    }
    return sprintf( '<ul>%s</ul>', $html );
    }
    add_shortcode('test_display','get_the_current_tax_terms_wpse');
    • This topic was modified 2 weeks, 3 days ago by baz74. Reason: typo
    • This topic was modified 2 weeks, 3 days ago by baz74.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter baz74

    (@baz74)

    For clarification – our Custom field settings are:
    Label: Venue
    Name: Relationship_venue
    Type: Relationship

    Which theme do you use and which PageBuilder? Depending on this, you can see what options you have here, even without individual programming.

    Thread Starter baz74

    (@baz74)

    we have TwentySeventeen theme and Oxygen.

    Sohil Vahora

    (@sohilvahora96)

    Hi, you can use below code to display taxonomy/category for each individual post inside posts loop,

    $venue_terms = get_the_terms(get_the_id(), 'Venue');
    if(!empty($venue_terms)){
    foreach($venue_terms as $venue_term){
    echo "<span>".esc_html($venue_term->name)."</span>";
    }
    }
    Thread Starter baz74

    (@baz74)

    thank you, I’ll try it if it’s the only option, but I was hoping not to have to learn/use php

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.